1, Encapsulation
- Encapsulation (information hiding): generally, direct access to the actual representation of data in an object should be prohibited and should be accessed through the operation interface
- Programming pursues "high cohesion and low coupling". High cohesion: the internal data operation details of the class are completed by themselves, and external interference is not allowed; Low coupling: only a small number of methods are exposed for external use
- Property private, get/set
Create a Student class
public class Student { //Properties are private and cannot be manipulated directly private String name; private int age; private char sex; private int id; //set/get is a method provided for external operations on these properties, //alt+insert -- getter s and setter s can automatically generate set/get methods according to properties public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } //Security check, setting default values public void setAge(int age) { if(age<0 || age>150){ this.age = -1; }else{ this.age = age; } } public int getId() { return id; } public void setId(int id) { this.id = id; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } }
Main program class Application
public class Application { public static void main(String[] args) { Student s1 = new Student(); System.out.println(s1.getName()); //null s1.setName("zhangsan"); System.out.println(s1.getName()); //zhangsan s1.setAge(70); System.out.println(s1.getAge()); //70 s1.setAge(180); System.out.println(s1.getAge()); //-1 } }
Finally, we briefly describe the role of encapsulation
- Implementation details of hidden code
- Improve program security
- Unified interface
- Increase the maintainability of the system
Extension: modifier public > protected > Default > private
2, Inherit
- Inheritance is a relationship between classes. In addition, the relationships between classes include dependency, composition and aggregation
- Two classes of inheritance relationship, one is called parent class (base class) and the other is called child class (derived class). Each parent class can have multiple subclasses, but each subclass can inherit only one parent class. For example, human beings are a parent class, and men and women are children inherited from human beings.
- Inheritance uses the keyword "extends"
- super and override
- All classes directly or indirectly inherit from the Object class by default
First define a Person class
//Parent class public class Person { public int money = 1_0000; public void say(){ System.out.println("speak"); } }
Then, define a Student class, which inherits from the Person class
//Subclass, inheriting all methods and properties of the parent class public class Student extends Person{ }
Finally, the main program class Application
public class Application { public static void main(String[] args) { Student s1 = new Student(); s1.say(); //speak System.out.println(s1.money); //10000 } }
Here, the attribute in the parent class is public, but generally it should be set to private (encapsulation), so we need to modify it
Person class
public class Person { //public int money = 1_0000; //If it is set to private, the subclass cannot directly operate the property, but can operate through the set/set method private int money = 1_0000; public void say(){ System.out.println("speak"); } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } }
Main program class
public class Application { public static void main(String[] args) { Student s1 = new Student(); s1.say(); //speak // System.out.println(s1.money); //10000 System.out.println(s1.getMoney()); //10000 } }
super
- super and this are used to distinguish properties and methods with the same name in subclasses and superclasses
Person class
//Parent class public class Person { public String name = "zhangsan"; public void print(){ System.out.println("person"); } }
Student class
//Subclass public class Student extends Person { private String name = "lisi"; public void print(){ System.out.println("student"); } public void test1(){ System.out.println(name); //lisi System.out.println(this.name); //lisi System.out.println(super.name); //zhangsan } public void test2(){ print(); //student this.print(); //student super.print(); //person } }
Main program class
public class Application { public static void main(String[] args) { Student s1 = new Student(); s1.test1(); System.out.println("======================="); s1.test2(); } }
Note:
- super can only appear in methods or constructors of subclasses
- super calls the constructor of the parent class, which must be in the first line of the subclass constructor
- super and this cannot call construction methods at the same time
- super(): Construction of parent class; this(): Construction of subclasses
If you don't understand, see the original video: Station B crazy God said
rewrite
- Why to override: Methods in the parent class and subclasses do not necessarily need or meet the requirements
- Rewriting is the rewriting of methods and has nothing to do with attributes
- Rewriting is the basis of polymorphism
- static methods cannot be overridden
Let's first look at the case of non rewriting, class B
public class B { public static void test(){ System.out.println("B==>test"); } }
Class A
public classA extends B{ public static void test(){ System.out.println("A==>test"); } }
Main program class
public class Application { public static void main(String[] args) { A a = new A(); a.test(); //A==>test B b = new A(); b.test(); //B==>test } }
Remove static and rewrite test method, class B
public class B { public void test(){ System.out.println("B==>test"); } }
Class A
public class A extends B{ // public void test(){ // System.out.println("A==>test"); // } //rewrite @Override //Annotation, functional annotation public void test() { super.test(); } }
Main program class
public class Application { public static void main(String[] args) { //Method invocation is only related to the data type on the left (the object has methods according to the data type) A a = new A(); a.test(); //A==>test //The reference of the parent class points to the subclass. If the subclass overrides the method, the method in the subclass of the subclass will be executed B b = new A(); b.test(); //A==>test } }
Note:
- There is an inheritance relationship, and the subclass inherits the methods of the parent class
- The method name must be the same and the method body must be different
- The parameter list must be the same (overload is the difference between parameter categories)
- The scope of modifiers can be expanded, but not narrowed: public > protected > Default > private (protected methods in the parent class can be rewritten as public methods in the child class)
3, Polymorphism
- Polymorphism: the same method can adopt many different behavior modes according to different sending objects
- The actual type of an object is determined, but there are many types of references that can point to an object
- Polymorphism is the polymorphism of methods, and there is no polymorphism of attributes
//Method invocation is only related to the data type on the left (the object has methods according to the data type) A a = new A(); a.test(); //A==>test //The reference of the parent class points to the subclass. If the subclass overrides the method, the method in the subclass of the subclass will be executed **//b cannot call a method that exists in a subclass but not in a parent class** B b = new A(); b.test(); //A==>test
Conditions for polymorphism:
- There is an inheritance relationship
- Subclasses override methods of the parent class
- A parent class reference points to a child class object