[JavaSE] Object-oriented encapsulation

The concept of encapsulation
Encapsulation is to surround the process and data, and access to the data can only be through the defined interface. Object-oriented computing begins with the basic concept that the real world can be represented as a series of fully autonomous, encapsulated objects that access other objects through a protected interface. Encapsulation is an information hiding technology that is implemented in Java through the keywords private, protected, and public.
What is encapsulation?
Encapsulation combines all the components of an object. Encapsulation defines how the program refers to the object's data. Encapsulation actually uses methods to hide the data of the class and control the extent to which users can modify the class and access the data. Proper encapsulation can make the program code easier to understand and maintain, and also enhances the security of the program code.
Characteristics of encapsulation
1. Data can only be accessed through specified methods.
2. Hide the instance details of the class to facilitate modification and implementation.
The specific steps to implement encapsulation are as follows:
1. Modify the visibility of the attribute to restrict access to the attribute, generally set to private .
2. Create a pair of assignment (setter) method and value (getter) method for each attribute, generally set to public, used for reading and writing attributes.
3. In the assignment and value acquisition methods, add attribute control statements (to judge the legality of attribute values).
The following uses the encapsulation of an employee class as an example to introduce the encapsulation process. We assume that the main attributes of an employee include name, age, and salary. Assume that the employee class is Employee. The example is as follows:

public class Employee {
    
    
    private String name; // 姓名
    private int age; // 年龄
    private double balance; // 工资
    
    //定义无参构造方法
    public Employee() {
    
    
    }
    //定义有参构造方法
    public Employee(String name, double balance, int age) {
    
    
        this.getBalance();
        this.getAge();
        this.getName();
    }
    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    public int getAge() {
    
    
        return age;
    }
    public void setAge(int age) {
    
    
        // 对年龄进行限制
        if (age > 18 && age < 45) {
    
    
            this.age = age;
        } else {
    
    
            System.out.println("年龄必须在18到45之间!");
            this. Age = 18; // 默认年龄
        }
    }
        public double getBalance() {
    
    
        	return balance;
    }

    public void setBalance(double balance) {
    
    
    	//工资必须大于1000
        if (balance>1000){
    
    
            this.balance = balance;
        }else {
    
    
            System.out.println("error");
        }
    }
}

As shown in the above code, the properties are modified using the private keyword, which means that no other class can access these properties except the Employee class itself. However, these properties can be assigned values ​​through their setXxx() methods and accessed through their getXxx() methods.
In the setAge() method of the age attribute, first judge the parameter age passed by the user. If the value of age is not between 18 and 45, set the age attribute value of the Employee class to 18, otherwise it will be the passed parameter. value.
Write the test class TestEmployee, call the setXxx() method of the Employee attribute in the main() method of the class to assign values ​​to its corresponding attributes, and call the getXxx() method to access the attributes. The code is as follows:

public class TestEmployee {
    
    
    public static void main(String[] args) {
    
    
        Employee people = new Employee();
        people.setName("小张");
        people.setAge(20);
        people.setBalance(10000);
        //输出信息
        System.out.println("姓名:" + people.getName());
        System.out.println("年龄:" + people.getAge());
        System.out.println("工资:" + people.getBalance());
    }
}

The role of encapsulation
① The data encapsulation feature of objects completely eliminates the problems caused by the separation of data and operations in the traditional structural method, improves the reusability and maintainability of the program, and reduces the programmer's burden of maintaining data and operation content. burden.
② The data encapsulation feature of the object can also separate the private data and public data of the object, protect the private data, reduce possible interference between modules, and achieve the purpose of reducing program complexity and improving controllability.

Guess you like

Origin blog.csdn.net/qq_58032742/article/details/132296052