1, Three characteristics of object-oriented programming
1.1 basic introduction
Object oriented programming has three characteristics: encapsulation, inheritance and polymorphism.
2, Object oriented programming - encapsulation
2.1 package introduction
2.2 understanding and benefits of packaging
2.3 implementation steps of packaging (three steps)
2.4 quick start cases
class Person { public String name; //Public name private int age; //age privatization private double salary; //.. //Constructor alt+insert public Person() { } //It's too slow to write setXxx and getXxx by ourselves. We use the shortcut key alt+insert //Then perfect our code according to the requirements public String getName() { return name; } public void setName(String name) { //Adding data verification is equivalent to adding business logic if(name.length() >= 2 && name.length() <=6 ) { this.name = name; }else { System.out.println("The length of the name is incorrect. It needs to be(2-6)Characters, default name"); this.name = "No celebrity"; } } public int getAge() { return age; } public void setAge(int age) { //judge if(age >= 1 && age <= 120) { //If it is a reasonable range this.age = age; } else { System.out.println("You set the wrong age, you need to (1-120), Give default age 18 "); this.age = 18;//Give a default age } } public double getSalary() { //You can add permission judgment for the current object here return salary; } public void setSalary(double salary) { this.salary = salary; }
3, Object oriented programming - inheritance
3.1 why inheritance
Improve code reusability.
3.2 basic introduction and schematic diagram of inheritance
Inheritance can solve code reuse and make our programming closer to human thinking. * * when multiple classes have the same attributes (variables) and methods, you can abstract the parent class from these classes, * * define these same attributes and methods in the parent class. All subclasses do not need to redefine these attributes and methods, but only need to declare the inherited parent class through extensions.
3.3 basic syntax of inheritance
3.4 quick start cases
//The parent class is the parent class of Pupil and Graduate public class Student { //Common attributes public String name; public int age; private double score;//Achievements / / common methods public void setScore(double score) { this.score = score; } public void showInfo() { System.out.println("Student name " + name + " Age " + age + " achievement " + score); } } //Subclass public class Pupil extends Student { public void testing() { System.out.println("pupil " + name + "I'm taking the math test in primary school.."); } } //Subclass public class Graduate extends Student { public void testing() {//Not like Pupil System.out.println("college student " + name + " I'm taking college math.."); } }
3.5 convenience of inheritance to programming
- The reusability of code is improved
- The expansibility and maintainability of the code are improved
3.6 in depth discussion / details of succession
- Subclasses inherit all properties and methods. Non private properties and methods can be accessed directly in subclasses, but private properties and methods cannot be accessed directly in subclasses. They should be accessed through public methods provided by the parent class.
- The subclass must call the constructor of the parent class to complete the initialization of the parent class.
- When creating a subclass object, no matter which constructor of the subclass is used, it will always call the parameterless constructor of the parent class by default. If the parent class does not provide a parameterless constructor, you must use super in the constructor of the subclass to specify which constructor of the parent class is used to complete the initialization of the parent class. Otherwise, the compilation will not pass.
public Sub() { //Parameterless constructor super(); //The default is to call the parameterless constructor of the parent class, which can be left blank System.out.println("Subclass Sub()Constructor called...."); }
- If you want to specify a constructor to call the parent class, explicitly call: Super (parameter list).
//When creating a subclass object, no matter which constructor of the subclass is used, the parameterless constructor of the parent class will always be called by default. If you want to modify it, use super to specify public Sub(String name) { super("tom", 30);//Call the constructor of the two parameters of the parent class //do nothing... System.out.println("Subclass Sub(String name)Constructor called...."); }
- When super is used, it must be placed in the first line of the constructor (super can only be used in the constructor).
- Both super() and this() can only be placed on the first line of the constructor, so the two methods cannot share a constructor.
- All java classes are subclasses of the Object class, and Object is the base class of all classes.
- The call of the parent constructor is not limited to the direct parent class! It will be traced up to the Object class (top-level parent class).
- A subclass can inherit at most one parent class (direct inheritance), that is, the single inheritance mechanism in java.
Thinking: how can class A inherit classes B and C? [A inherits B, B inherits C] - Inheritance cannot be abused. The logical relationship of is-a must be satisfied between subclasses and parent classes.
3.7 essential analysis of inheritance (important)
/** * Explain the essence of inheritance */ public class ExtendsTheory { public static void main(String[] args) { Son son = new Son(); //Memory layout //?-> At this time, please note that the information should be returned according to the search relationship //(1) First, check whether the subclass has this attribute //(2) If the subclass has this property and can be accessed, information is returned //(3) If the child class does not have this attribute, it depends on whether the parent class has this attribute (if the parent class has this attribute and can access it, it returns information..) //(4) If there is no parent class, continue to find the parent class according to the rule in (3) until Object System.out.println(son.name);//Return is big head son //System.out.println(son.age);// The return is 39 //System.out.println(son.getAge());// The return is 39 System.out.println(son.hobby);//Return is tourism } } class GrandPa { //Ye class String name = "Big head grandpa"; String hobby = "Travel"; } class Father extends GrandPa {//Parent class String name = "Big head Dad"; private int age = 39; public int getAge() { return age; } } class Son extends Father { //Subclass String name = "Big head son"; }
Four super keywords
4.1 basic introduction
super represents the reference of the parent class and is used to access the properties, methods and constructors of the parent class
4.2 basic grammar
//Super's access is not limited to the direct parent class. If there are members with the same name in the grandfather class and this class, you can also use super to access the members of the grandfather class; // If there are members with the same name in multiple base classes (parent classes), super access follows the proximity principle. A->B->C
4.3 convenience / details brought by super to programming
4.4 comparison between super and this
This blog quotes Mr. Han Shunping's Java course