[Yugong series] December 2021 Java Teaching Course 24 - object oriented encapsulation

Posted by cbesh2 on Sun, 02 Jan 2022 17:23:49 +0100

1, Encapsulation

1.private keyword

Overview: private is a modifier that can be used to modify members (member variables, member methods)

Features: members modified by private can only be accessed in this class. If private modified member variables need to be used by other classes, corresponding operations are provided

The "get variable name ()" method is provided to obtain the value of the member variable. The method is decorated with public

The set variable name (parameter) method is provided to set the value of the member variable. The method is decorated with public

Example code:

/*
    Student class
 */
class Student {
    //Member variable
    String name;
    private int age;

    //Provide get/set methods
    public void setAge(int a) {
        if(a<0 || a>120) {
            System.out.println("You gave the wrong age");
        } else {
            age = a;
        }
    }

    public int getAge() {
        return age;
    }

    //Member method
    public void show() {
        System.out.println(name + "," + age);
    }
}
/*
    Student testing
 */
public class StudentDemo {
    public static void main(String[] args) {
        //create object
        Student s = new Student();
        //Assign values to member variables
        s.name = "Lin Qingxia";
        s.setAge(30);
        //Call the show method
        s.show();
    }
}

2. Use of private keyword

  • Requirements:

    • Define standard student classes and require name and age to be decorated with private
    • It also provides set and get methods and show methods that are convenient for displaying data
    • Create an object in the test class and use it. The final console output is Lin Qingxia, 30
  • Example code:

    /*
        Student class
     */
    class Student {
        //Member variable
        private String name;
        private int age;
    
        //get/set method
        public void setName(String n) {
            name = n;
        }
    
        public String getName() {
            return name;
        }
    
        public void setAge(int a) {
            age = a;
        }
    
        public int getAge() {
            return age;
        }
    
        public void show() {
            System.out.println(name + "," + age);
        }
    }
    /*
        Student testing
     */
    public class StudentDemo {
        public static void main(String[] args) {
            //create object
            Student s = new Student();
    
            //Assign values to member variables using the set method
            s.setName("Lin Qingxia");
            s.setAge(30);
    
            s.show();
    
            //Use the get method to get the value of the member variable
            System.out.println(s.getName() + "---" + s.getAge());
            System.out.println(s.getName() + "," + s.getAge());
    
        }
    }
    

3. this keyword [application]

Overview: this modified variable is used to refer to member variables. Its main function is to (distinguish the problem of duplicate names of local variables and member variables)

  • If the formal parameter of the method has the same name as the member variable, the variable without this modifier refers to the formal parameter, not the member variable
  • The formal parameter of the method does not have the same name as the member variable. The variable without this modifier refers to the member variable

Code implementation:

public class Student {
    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void show() {
        System.out.println(name + "," + age);
    }
}

4. Principle of this memory [understanding]

  • Note: This represents the reference of the current calling method, the method of which object is invoked, and which object this represents.

  • Illustration:

5. Packaging ideas

  1. Packaging overview
    Is one of the three characteristics of object-oriented (encapsulation, inheritance, polymorphism)
    It is the simulation of the objective world by object-oriented programming language. In the objective world, the member variables are hidden inside the object, and the outside world can not be operated directly
  2. Encapsulation principle
    Some information of the class is hidden inside the class, which is not allowed to be directly accessed by external programs. Instead, the operation and access of hidden information are realized through the methods provided by the class
    The member variable private provides the corresponding getXxx()/setXxx() methods
  3. Packaging benefits
    Methods are used to control the operation of member variables, which improves the security of the code
    The code is encapsulated by method, which improves the reusability of the code

Topics: Java Back-end