1. Encapsulation (key)
Three characteristics of object-oriented: encapsulation, inheritance and polymorphism
//Object oriented case: car starting
//Analysis: Category: vehicle category Object: new Attributes: color, brand, etc Method: start
//Problem: the price can be assigned as a negative number -- there is no problem with the program logic, but the data is unreasonable
//Solution: judge whether the value of the attribute is greater than 0 in the program. If it is less than 0, give a default value
//Encapsulation: do not directly assign and value attributes, but encapsulate them through methods; This method is the set/get method
//Benefits: improved security and reusability
//Encapsulation operation steps:
//1. Write set/get methods
//2. Attribute privatization
class Car{ private String brand; //Write set and get Private: private permission. The restricted attribute can only be used in the current class private int price; /*public void start() { System.out.println(brand+"Brand cars are starting; The price is: "+ price"; }*/ //set specification: set + attribute name is capitalized, and the parameter name is consistent with the attribute name public void setPrice(int price) { //Note: This is a logical judgment for the purpose of explaining the packaging characteristics; if there is no special description later, it will be assigned directly if(price<0) { this.price = 100000; //Default value for given exception data }else { this.price = price; } } //Get specification: get + initial capitalization of attribute name public int getPrice() { return price; //Can I + this? Yes, but if there is no conflict in the get method, it can be omitted } public void setBrand(String brand) { this.brand = brand; } public String getBrand() { return brand; } } public class Test { public static void main(String[] args) { Car car = new Car(); car.setBrand("red flag"); car.setPrice(-1000); System.out.println("---------"); //car.price = -10000; // Weighted limit limit -- avoid the entry of abnormal data from the source car.setPrice(-10000); System.out.println(car.getPrice()+"==>"+car.getBrand()); //Value } }
2. Succession (key)
Inheritance in life: the gift of the donor and the acquisition of the recipient
Inheritance in the program: two classes meet the "is a" relationship. We can set such two classes as inheritance
Features: with inheritance relationship, the subclass has the properties and methods of the parent class
Selection of parent class:
There are many classes that satisfy the is a relationship in life. We need to choose the most appropriate parent class
For example, dogs are animal, biological and material
Direct parent selection of animals - Characteristics and behavior are closest
Conclusion: the more coincidence points, the closer to the direct parent
The fewer coincidence points, the closer to the Object class (ancestral class)
Inherited applications:
Case: subclasses include dogs and birds. Find out their characteristics and behavior
Dogs:
Features: [variety, name, age], hair color
Behavior: [eat, sleep], run
Fish: [species, name, age]
Features: [eat, sleep], swim
Parent class: animal class - commonality extraction, put the common characteristics and behaviors of all classes into the parent class, and the child class only needs to inherit
Characteristics: variety, name, age
Behavior: eat, sleep
Function: simplify the amount of code and improve reusability
============================Inheritance commonality extraction==========================
public class Animal { String brand; String name; int age; public void eat() { System.out.println(name+"I am eating"); } public void sleep() { System.out.println(name+"Sleeping"); } } //The Dog class inherits the Animal class class Dog extends Animal{ String color; public void run() { System.out.println(name+"Running.."); } } //The Fish class inherits the Animal class class Fish extends Animal{ public void swim() { System.out.println(name+"Swimming..."); } } public class Test { public static void main(String[] args) { Dog dog = new Dog(); //Instantiate subclass objects dog.name = "Wangcai"; //Characteristics of calling parent class dog.color = "yellow"; //Characteristics of this class dog.eat(); //Behavior of the calling parent class dog.run(); //Call this kind of behavior //Anonymous object: when an object without a name is operated only once, the method can be called directly from the new object (anonymous object) //new Animal().run(); // Note: the parent class cannot adjust the characteristics and behavior of the child class } }
============================Multi level inheritance cases===========================
//Details:
//Multiple inheritance (multiple fathers) is not allowed, but multi-level inheritance (including parent class, Grandpa class, taigrandpa class...)
//The characteristics and behaviors of multi-level inheritance can be superimposed
//Case:
//Grandpa owns 1 million, his father owns a BMW and his son owns an Altman
class GrandFather{ public void haveMoney() { System.out.println("With 1 million"); } } class Father extends GrandFather{ //Son inherits GrandFather indirectly public void haveCar() { System.out.println("Owning a BMW"); } } class Son extends Father{ //Son inherits Father directly public void havePeron() { System.out.println("Own Altman"); } } public class Test { public static void main(String[] args) { Son son = new Son(); son.havePeron(); //Method of tone subclass itself son.haveCar(); //Calling parent method son.haveMoney(); //Call indirect parent method } }
============================Non inheritability===========================
1. The private characteristics and behaviors of the parent class cannot be inherited
2. The constructor cannot be inherited
3. Some permission restricted features and behaviors in different scenarios cannot be inherited (test description)
=========================Non inheritability case 1========================
//Cannot be inherited:
//1. The private characteristics and behaviors of the parent class cannot be inherited
//2. The construction method cannot be inherited ---- it is best to test the construction with parameters
public class Person { private String name; //Private property public Person() {} public Person(String name) { } } class Student extends Person{ public void study() { //System.out.println(name); // Private permission, which cannot be inherited in subclasses } } public class Test { public static void main(String[] args) { //If the parent class is constructed with parameters but the child class is not, an error will be reported if the child class calls the parent class to construct with parameters. This indicates that the construction method has no inheritance //Student st = new Student("666"); } }
===========================Permission test==========================
In the object-oriented case of java, the main permissions are: private, default, interpreted, public
//Cannot be inherited:
//1. The private characteristics and behaviors of the parent class cannot be inherited
//2. The construction method cannot be inherited ---- it is best to test the construction with parameters
//3. Some permissions in different scenarios cannot be inherited (test description)
public class Person { private String name; //Private property int age; //default permissions protected String sex; //Protection Authority public String love; //Public authority public Person() {} public Person(String name) { } public void eat() { System.out.println(name); //Private permission, which can be used in this class System.out.println(age); //Default permission, which can be used in this class } } class Student extends Person{ public void study() { //System.out.println(name); // Private permission, which cannot be inherited in subclasses } } class MyTest{ public static void main(String[] args) { Person p = new Person(); //System.out.println(p.name); / / private permission, which cannot be used in different classes of the same package System.out.println(p.age); //Default permission, which can be used in different classes of the same package System.out.println(p.sex); //Protection permissions can be used in different classes of the same package System.out.println(p.love); //Public permissions can be used in different classes of the same package } }
----------Here is the class of another package------------
public class Teacher extends Person { public void work() { //System.out.println(name); // Private permission, which cannot be used in subclasses of different packages //System.out.println(age); // Default permission, which cannot be used in subclasses of different packages System.out.println(sex); //Protection permission, which can be used in subclasses of different packages System.out.println(love); //Public permissions can be used in subclasses of different packages } } class Car{ public void start() { //System.out.println(love); //this.love } public static void main(String[] args) { Person person = new Person(); //System.out.println(person.name); / / private permission, which cannot be used in different packages without inheritance relationship //System.out.println(person.age); // The default permission cannot be used in different packages without inheritance relationship //System.out.println(person.sex); // Protection permission cannot be used in different packages without inheritance relationship System.out.println(person.love); //Public permissions can be used in different packages without inheritance } }
3. Inheritance rewriting (key and difficult points)
//Case: father and son eat differently
//Overview: the methods of the subclass are exactly the same as those of the parent class (return value type, method name, parameter type)
//The permission of the child class is not less than that of the parent class (generally public)
//Application scenario: when the method of the parent class is not sufficient for the child class, the child class can override the parent class method
class Father{ public void eat() { System.out.println("Very gentlemanly..."); } } class Son extends Father{ @Override //Rewrite the annotation. Mark that the following method is the rewriting method. If it is not, an error will be reported public void eat() { System.out.println("Devour..."); } } public class Test { public static void main(String[] args) { Son son = new Son(); son.eat(); } }
4. super use
Review: usage of this -- current object
this. Attribute
this. Method
This tone construction method: this() This (parameter)
super: parent object
super. Attribute: the attribute of the parent class object
super. Method: Method of calling parent class object
super() or super (parameter): the constructor of the calling parent class
Note: in the inheritance relationship, if this does not have its own properties and methods, the parent class will be called according to the inheritance
=========================super tuning method========================
class Son extends Father{ /* public void haveMoney() { System.out.println("Have 100 pieces "); }*/ public void have() { System.out.println("Own Altman"); //super.haveMoney(); // Call the method of the parent class this.haveMoney(); //Method of adjusting itself } } public class SuperMethod { public static void main(String[] args) { new Son().have(); } }
=========================super call attribute========================
class Fu{ String name="Miss Luo Yu feng"; } class Zi extends Fu{ String name = "Liu Yifei"; public void test(){ System.out.println(super.name); //Call the properties of the parent class System.out.println(this.name); //If you have attributes, you can adjust your own. If you don't, you can adjust the parent class System.out.println(name); //Equivalent to this.name } } public class SuperField { public static void main(String[] args) { Zi zi = new Zi(); zi.test(); } }
=========================super tone construction method========================
//Subclass instantiation process: //Case: C inherits B, B inherits A, instantiates the object of C, and view the instantiation process //Analysis: when calling a subclass construction method, super() will be placed in the first sentence by default in the construction method; it means that the parent class is called to construct without parameters first; //So finally, print the parameterless structure of the parent class first, and then print the child class structure //In inheritance, when a subclass is instantiated, why is super() called by default? Because the parent resource needs to be loaded first; the subclass can have the parent resource class A /*extends Object*/{ //If there is no inherited parent class, Object is directly inherited public A() { super(); //---Parameterless construction of Object System.out.println("A Nonparametric structure of"); } } class B extends A{ public B() { super(); System.out.println("B Nonparametric structure of"); } } class C extends B{ public C() { super(); System.out.println("C Nonparametric structure of"); } } public class SuperTest1 { public static void main(String[] args) { C c = new C(); } }
=========================Construction method of super and this========================
class AA{ public AA(){ System.out.println("AA Nonparametric structure"); } public AA(String name) { System.out.println("AA Parametric structure"); } } class BB extends AA{ public BB() { System.out.println("BB Nonparametric structure"); } public BB(String name) { //super(); //super(name); this(); //Will super() be generated by default Because both super() and this() appear in the first sentence -- conflict //Conclusion: this() and super() cannot coexist System.out.println("BB Parametric structure"); } } public class SuperTest2 { public static void main(String[] args) { //new BB(); // AA no reference BB no reference new BB("666"); //AA no reference BB with reference [AA tape BB [with reference] AA no reference BB no reference BB with reference } }