Java encapsulation details

Java classes and objects

This article is the author's notes and experience in learning station B series video "crazy God talking about Java" and the classic book "Java core technology". It is not easy to create. I hope I can get your support.

Java constructor

  • Java constructor
    When using java to customize a class, even if there is no content in the class, there will be a method, which is the Java constructor
public class Application
{
    public static void main(String[] args)
    {
        Person person = new person();
        //new instantiates an object
        System.out.println(person.name);
        //Leon
    }
}
public class Person()
{
    String name;
    public Person()
    {
        this.name="Leon";
    }
}

You can see that after instantiating a person object with the news keyword, we assign a value to name and print it through a function (method) that does not return a value. This function is the constructor in Java.

Description of constructor:

  1. The constructor has the same name as the class. When constructing a Person object, the constructor will run automatically. If we don't specifically write out the constructor and add statements to it, the constructor will run, but it won't have any impact.
  2. The constructor differs from other methods in that it must be called in conjunction with the new keyword. If the object already exists, you cannot call the constructor to reset the instance field.
  3. Constructor has no return value type (no need to write void).
  4. The intrinsic function of constructors is to initialize values.
  5. There can be many constructors, but the parameters of each constructor must be different; Once a parameterized constructor is defined, if you still want to use a parameterless constructor, you must explicitly define a parameterless constructor, otherwise the compiler will report an error. The compiler will determine which constructor to call according to the actual parameter list at the time of call.
public class Application
{
    public static void main(String[] args)
    {
        Person person1 = new person();
        Person person2 = new person("ZHA")
        //new instantiates an object
        System.out.println(person1.name);
        //Leon (parameterless construction called)
        System.out.println(person2.name)
        //Zha (parameterized construct called)
        
    }
}
public class Person()
{
    String name;
    public Person()
    {
        this.name="Leon";
    }
    public Person(String name)
    {
        this.name=name;
    }
}

encapsulation

The pursuit of program design: high cohesion (the internal data operation details of the class are completed by ourselves), low coupling (exposing a small number of methods to external use)

public class Student{
    //Name, and the private keyword is the private attribute
    private String name;
    //Student number
    private int age;
    public String getName()
    {
        return this.name;
    }//Method to get name
    public void setName(String name)
    {
        this.name=name;
    }//Method of setting name
    public int getAge()
    {
        return this.age;
    }
    public void setAge(int age)
    {
        if(age>120||age<0>)//wrongful
        {
            this.age=3;
        }
        else
        {
            this.age=age;
        }
    }
}
public class Application{
    Student s1=new Student();
    // s1.name="xxx";// An error will be reported because the name cannot be accessed externally due to the private attribute
    s1.setName("Leon");
    System.out.println(s1.getName());
    //Output Leon
    s1.setAge(30);
    System.out.println(s1.getAge());//30
    s1.setAge(130);
    System.out.println(s1.getAge());//3
}

Explanation of encapsulation:

  1. As shown above, the getName and getAge methods only return the value of the instance field, so they are also called field accessors

  2. Age is a read-only field. If it is set in the constructor, there is no way to modify it to ensure that the age field will not be damaged by the outside world

  3. If you want to access the value of an instance field, you need a private data, a public field accessor method (get...) and a public field changer method (set...)

  4. The modifier can check the error. Check the age with the above code. If age > 120 or age < 0, then age is illegal data, and the system will automatically return age=3

  5. If you define a field with the final keyword, the value of this field cannot be modified, that is, the modifier (set...) will not work

Tags: Java

Posted on Mon, 20 Sep 2021 22:25:21 -0400 by hasin