One of the three major features of Java (encapsulation)

encapsulation

  • General requirements for program design: "high cohesion, low coupling" . High cohesion means that the internal data operation details of the class are completed by itself, and no external interference is allowed; low coupling: only a small number of methods are exposed for external use.

  • Encapsulation (hiding of data)

    • In general, direct access to the actual representation of data in an object should be prohibited, and should be accessed through the operational interface, which is called information hiding.

  • Remember one sentence: attribute private, get/set

//Property private, get/set 
​package
com.oop.Demo3; 
//Student class 
public class Student { 
    //Property private use private 
    private String name; //Name 
    private int id; //Student number 
    private int age; / /age 
    private char sex; //gender 
    /* 
    Provide some methods that can manipulate these attributes 
    Provide some public get or set methods 
     */ 
    //get or set 
​//
    Alt key + Insert key can quickly generate set and get methods 
    //get Get attribute variable name 
    //set Set value for attribute variable name 
    
    public String getName() { 
        return name; 
    } 
    public void setName(String name) { 
        this.name = name; 
}
    
    public int getId() {
        return id;
    }
​
    public void setId(int id) {
        this.id = id;
    }
​
    public int getAge() {
        return age;
    }
​
    public char getSex() {
        return sex;
    }
​
    public void setSex(char sex) {
        this.sex = sex;
    }
​
    public void setAge(int age) {
        if(age>150 || age <0){
            this.age = 3;
        }else {
            this.age = age;
        }
​
    }
​
​
}
​

 

The role of packaging

/*The role of encapsulation 
1. Improve the security of the program and protect the data 
2. The implementation details of the hidden code 
3. Unify the interface 
4. Improve the maintainability of the program 
 */
package com.oop; 
import com.oop.Demo3.Student; 
/*The function of encapsulation 
1. Improve program security and protect data 
2. Hidden code implementation details 
3. Unify interface 
4. Improve program maintainability 
 * / 
​​public

class Application { 
    public static void main(String[] args) { 
        Student s1 = new Student(); 
        s1.setName("Chen Binbin"); 
        s1.setId(666); 
        s1.setAge(29); 
        s1 .setSex('male'); 
​String
        name = s1.getName(); 
        int id = s1.getId(); 
        char sex = s1.getSex(); 
        int age = s1.getAge(); 
        Class<? extends Student > s1Class = s1.getClass(); 
        System.out.println("Name: "+name);
        System.out.println("Student number: "+id); 
        System.out.println("Gender:"+sex); 
        System.out.println("Age: "+age); 
        System.out.println(" Class name: "+s1Class); 
    } 
}

Guess you like

Origin blog.csdn.net/weixin_40294256/article/details/117322558