Three features of java_ Encapsulated learning

Encapsulation:
In the object-oriented programming method, Encapsulation refers to a method to wrap and hide the implementation details of abstract function interfaces.

Encapsulation can be considered as a protective barrier to prevent the code and data of this class from being randomly accessed by the code defined by the external class.

To access the code and data of this class, it must be controlled through strict interface.

The main function of encapsulation is that we can modify our implementation code without modifying the program fragments that call our code.

Proper encapsulation can make the code easier to understand and maintain, and also enhance the security of the code.

I Advantages of packaging
1. Good packaging can reduce coupling.

2. The internal structure of the class can be modified freely.

3. You can control the member variables more accurately.

4. Hide information and realize details.

II Steps to realize JAVA encapsulation:

  1. The property access permission is generally set to private

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

In this code, the , name , and , age , attributes are set to private, which can only be accessed by this class, and can not be accessed by other classes, so the information is hidden.

2. Provide external public method access for each value attribute, that is, create a pair of value assignment methods for accessing private attributes, for example:

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

  public void getName(){
         return name;
       }
  public void getAge(){
        return age;
      }
  public void setName(String name){
       this.name=name;
 }
  public void setAge(int age){
       this.age=age;
  }
}

The set get method generates the shortcut key alt+shift+s

 

 

The keyword "this" is used to solve the conflict of the same name between the instance variable (private String name) and the local variable (name variable in setName(String name)).

//Private is an access modifier. Properties or methods decorated with private can only be used inside this class

//I want to get it alone age Attribute value
        System.out.println(stu.getAge());

 

Case:

 

Exercise 1 (Basics):

Define a class Student that represents Student information. The requirements are as follows:

(1) Member variables of class Student:

sNO means student number; sName means name; sSex indicates gender; Age is age; sJava: indicates the grade of Java course.

(2) Construction method of class Student with parameters:

In the construction method, the assignment of member variables is completed through formal parameters.

(3) Method members of class Student:

getNo(): obtain student number;  

getName(): get the name;  

getSex(): obtain gender;

getAge() gets the age;

getJava (): get the score of Java course

According to the definition of class Student, create five objects of this class, output the information of each Student, calculate and output the average value of the five students' Java language scores, and calculate and output the maximum and minimum values of their Java language scores.

 

 1 package day07;
 2 
 3 public class Student {
 4 
 5 private    int sNo;
 6 private    String SName;
 7 private    String sSex;
 8 private    int sAge;
 9 private    double sJava;
10     
11     
12     
13  public Student(int sNo,String SName,String sSex,int sAge,double sJava){
14     this.sNo=sNo;
15     this.SName=SName;
16     this.sSex=sSex;
17     this.sAge=sAge;
18     this.sJava=sJava;
19      
20      
21  }
22 
23  
24 
25 
26 public int getsNo() {
27     return sNo;
28 }
29 public void setsNo(int sNo) {
30     this.sNo = sNo;
31 }
32 public String getSName() {
33     return SName;
34 }
35 public void setSName(String sName) {
36     SName = sName;
37 }
38 public String getsSex() {
39     return sSex;
40 }
41 public void setsSex(String sSex) {
42     this.sSex = sSex;
43 }
44 public int getsAge() {
45     return sAge;
46 }
47 public void setsAge(int sAge) {
48     this.sAge = sAge;
49 }
50 public double getsJava() {
51     return sJava;
52 }
53 public void setsJava(int sJava) {
54     this.sJava = sJava;
55 }
56  
57 
58 //public static void main(String[] args) {
59 //    Student[] stu=new Student[5];
60 //    stu[0] =new Student(10,"Zhang San","male",18,68);
61 //    stu[1] =new Student(11,"Li Si","female",18,98);
62 //    stu[2] =new Student(12,"Zhang San","male",18,68);
63 //    stu[3] =new Student(13,"Zhang San","male",18,88);
64 //    stu[4] =new Student(14,"Zhang San","male",18,75);
65 //    
66 //    int max=(int) stu[0].sJava;
67 //    int min=(int) stu[0].sJava;
68 //    int avg=0;
69 //    int sum=0;
70 //    for(int i=0;i<stu.length;i++){
71 //        sum=sum+(int) stu[i].sJava;
72 //        avg=sum/stu.length;
73 //        
74 //          if(max<stu[i].getsJava()){
75 //             max=(int) stu[i].getsJava();
76 //          }if(min>stu[i].getsJava()){
77 //              min=(int) stu[i].getsJava();
78 //           }
79 //          
80 //          
81 //          }
82 //    System.out.println("The maximum value is"+max);
83 //    System.out.println("The minimum value is"+min);
84 //    System.out.println("The average score is"+avg);
85 //    }
86 
87 }

Test class call:

 1 package day07;
 2 
 3 public class TestStudent {
 4 
 5     public static void main(String[] args) {
 6         
 7         Student[] stu=new Student[5];
 8         stu[0] =new Student(10,"Zhang San","male",18,68);
 9         stu[1] =new Student(11,"Li Si","female",18,98);
10         stu[2] =new Student(12,"Zhang San","male",18,68);
11         stu[3] =new Student(13,"Zhang San","male",18,88);
12         stu[4] =new Student(14,"Zhang San","male",18,75);
13         
14         int max=(int) stu[0].getsJava();
15         int min=(int) stu[0].getsJava();
16         int avg=0;
17         int sum=0;
18         for(int i=0;i<stu.length;i++){
19             sum=sum+(int) stu[i].getsJava();
20             avg=sum/stu.length;
21             
22               if(max<stu[i].getsJava()){
23                  max=(int) stu[i].getsJava();
24               }if(min>stu[i].getsJava()){
25                   min=(int) stu[i].getsJava();
26                }
27               
28               
29               }
30         System.out.println("The maximum value is"+max);
31         System.out.println("The minimum value is"+min);
32         System.out.println("The average score is"+avg);
33         }
34     }
35 
36     

The basic concept comes from https://www.runoob.com/java/java-encapsulation.html

Posted by neogeek on Mon, 16 May 2022 23:01:55 +0300