<>1 inherit

<>1.1 concept

Inheritance is one of the most significant characteristics of object-oriented
Inheritance is to derive a new class from an existing class , The new class can absorb the data properties and behaviors of the existing class , And expand new capabilities .
Java Inheritance is a technology that creates new classes based on the definitions of existing classes
The definition of a new class can add new data or new functions , You can also use the functions of the parent class , But you can't selectively inherit the parent class ( Superclass / Base class )
This inheritance makes it easy to reuse previous code , Can greatly shorten the development cycle , Reduce development costs .

<>1.2 characteristic

* use extends Keyword to represent the inheritance relationship
* It is equivalent to that the subclass copies the function of the parent class
* Java Only single inheritance is supported
* Inheritance is transitive ( grandpa / Son / Such a relationship )
* Cannot inherit private members of a parent class
* Inheritance is often used to modify functions , A subclass can have the function of a parent class at the same time , Function expansion
* It's like is a The relationship between
<>1.3 practice : Introduction to inheritance

create package : cn.tedu.oopextends
Create class : TestExtendsDemo.java
package cn.tedu.oopextends; /** This class is an introductory case of inheritance */ public class TestExtendsDemo {
//6. Create the entry function of the program public static void main(String[] args) { //7. establish Cat Class object Cat c =
new Cat(); c.eat(); MiaoMiao m = new MiaoMiao(); m.eat(); } } //1. Create parent class Animal-- grandpa
class Animal{ //2. Define a common method public void eat() { System.out.println(" Small animals can eat anything ~"); }
} //3. establish Animal Subclasses of classes Cat-- dad /**1. The subclass and the parent are inherited , Use keywords extends To express / connect */
/**2.java Only single inheritance is supported , A subclass can only have one parent , But a parent class can have more than one subclass */ class Cat extends Animal{ //5. Define attributes
int a = 10; private int b = 100; } //4. establish Cat Subclasses of classes MiaoMiao-- grandson
/**5. Inheritance is transitive , Grandfather's function will be passed on to Dad , Father's function will be passed on to grandson */ /**6. Succession is is
a The relationship between , An inheritance structure that requires a subclass to be a parent , It's very dependent , Strong coupling */ class MiaoMiao extends Cat{ //8. Create your own unique method in the subclass
public void print() { /**3. Subclass inherits from parent , It is equivalent to copying the functions of the parent class */ System.out.println(a);
// Subclasses can print non private properties of the parent class a /**4. Private resources of the parent class , Subclass is not inheritable */
//System.out.println(b);// Subclasses cannot print private property values of the parent class b } }
<>2 super

You can use this keyword to use the content of the parent class ,Super Represents a reference object of the parent class
be careful : In the construction method , The call position that appears must be the first line

<>3 Method override Override

* After inheritance , The subclass has the function of the parent
* In subclasses , You can add subclass specific functions , You can also modify the original function of the parent class
* When the signature of a method in a subclass is exactly the same as that of the parent class , Coverage will occur / The phenomenon of copying
* Format requirements : Method Method name parameter list To be exactly the same , The method body is rewritten
be careful : Private methods of a parent class cannot be overridden , When a subclass overrides a parent class method , Modifier
When a subclass overrides a parent method , The child modifier must be greater than or equal to the permission of the parent modifier
<>4 The usage of inheritance

<>4.1 practice : The use of member variables

create package : cn.tedu.oopextends
Create class : TestExtends1.java
package cn.tedu.oopextends; /** This class is used to test the use of member variables in inheritance */ public class TestExtends1 {
//5. Create the entry function of the program main public static void main(String[] args) { //6. Create an object to test Son s =
new Son(); s.eat(); } } //1. Create parent class class Father{ //7. Defining properties in a parent class int sum = 1000; int
count= 888; } //2. Creating subclasses Son class Son extends Father{ //4.2 Define member variables int sum = 100;
//3. Common ways to define subclasses public void eat() { //4.1 Defining local variables of subclasses in common methods int sum = 10; System.out.
println(sum);//10, The principle of variable proximity , Local variables are printed System.out.println(this.sum);
//100, Using this Specified member variable sum /** Using the parent's sum resources , Need to use super. Make the assignment * super Is an object reference that represents a parent class *
It can be understood as Father super = new Father(); * */ System.out.println(count);// The resources of the parent class are used
System.out.println(super.sum); //1000, Using super The member variable of the specified parent class sum } }
<>4.2 practice : Use of member methods

create package : cn.tedu.oopextends
Create class : TestExtends2.java
package cn.tedu.oopextends; /** This class is used to test the use of member methods in inheritance */ // summary :
//1. The significance of method rewriting : Without modifying the source code , Complete the business modification //2. Rewriting requirements : Method declaration of subclass ( return type Method name ( parameter list ) ) As like as two peas
//3. Rewriting does not change the original business of the parent class , Only the business of the subclass has been modified public class TestExtends2 { public static void
main(String[] args) { //3. Create subclass objects for testing Son2 s = new Son2();
/**1. The subclass inherits the parent , You can use all the functions of the parent class */ s.eat();
/**2. After Succession , Subclasses can not only use the functions of the parent class , You can also have your own unique functions , Realize the expansion of functions */ s.study(); } } //1. Create parent class class
Father2 { public void eat() { System.out.println(" Dad loves meat "); } } //2. Creating subclasses class
Son2 extends Father2{ //4.1 Provides subclass specific functions public void study() { System.out.println(
" It's almost new year , Learn to make dumplings !"); } /**OCP principle : Close for modification , Open to the outside world -- Only function expansion is allowed , Modifying the original code is not allowed */
//5. Modify the function of the parent class -- It is not allowed to modify the original business of the parent class directly // Function modification -- Method rewriting Override
/**3. rewrite : And the method signature of the parent class [ return type & Method name & parameter list ] bring into correspondence with void eat() As like as two peas , The modification is the function of subclass , The function of the parent class has not changed */
/**4. When rewriting , A subclass must have permission to override the function of its parent , A private method of a parent class cannot be overridden * Modifier for method >= Modifier for parent class * */ public void eat()
{ System.out.println(" My son loves vegetables "); } }
<>4.3 practice : Use of construction methods

create package : cn.tedu.oopextends
Create class : TestExtends3.java
package cn.tedu.oopextends; /** This class is used to test the use of construction methods in inheritance */ // summary :
//1. When subclasses create objects , By default, the parent class's nonparametric construction is accessed //2. There is a default statement on the first line of the subclass constructor super()-- Calling a parent class's parameterless construction
//3. When the parent class has no parameterless construction , Can pass super Call other construction methods of the parent class //4. Constructors cannot be inherited ! public class TestExtends3 {
//3. Create the entry function of the program main public static void main(String[] args) { //4. Create parent object //Father2
f = new Father2(" braised pork in brown sauce "); //6. Creating subclass objects Son2 s = new Son2(); } } //1. Create parent class class
Father2{ /**0. Can constructor be inherited ?-- may not !!! * The requirements of grammatical structure : The method name of the construction method should be consistent with the class name of this class , Naturally, it doesn't meet the requirements * */
//5. Manually add a parameterless construct of a parent class // public Father2() { // System.out.println(" I am Father2 The nonparametric construction of ");
// } //8. Creating a parametric construction of a parent class public Father2(String s) { System.out.println(
" I am Father2 Parametric structure of the matrix "+s); } } //2. Creating subclasses class Son2 extends Father2{ //7. How to create subclasses
public Son2() { /**1. In the construction method of subclass , Default exist super(), So when you create subclass objects , By default, the nonparametric construction of the parent class is called */
/**2. When subclasses create objects , The parameterless construction of the subclass is called automatically * But after the succession , Will call the parent class's nonparametric construction first , Then perform the functions in your own construction method * */
/**3. When there is no parameterless construction in the parent class , Call the parent class's construction with parameters * You must call the constructor of a parent class */ super(" braised pork in brown sauce "); System.out.println
(" I am Son2 The nonparametric construction of "); } }
<>5. expand

<>5.1 this And super The difference between

* This Represents a reference to an object of this class
class Cat{ this.XXX } It can be understood as : Cat this = new Cat();
* super Represents a reference to a parent object
class Father{ }
class Son extends Father{ super.XXX }
It can be understood as : Father super = new Father();
* this It can be used when two variable names are the same , Used to distinguish between member variables and local variables
* this It can be called between the construction methods of this class , The position must be the first statement , be careful , Cannot call each other , It's a dead cycle
* super After the succession , If the subclass wants to use the function of the parent class , Can pass super call
* If rewriting happens , I also want to use the function of the parent class , Need to use super To call
* Super When a parent class construction method is called , Must appear in the first statement of a subclass constructor , And if no parameterless construction is provided in the parent class , Subclasses can be super To call other parameterized constructs of the parent class
<>5.2 heavy load Overload And rewriting Override The difference between

* heavy load : A phenomenon in a class , There are many methods with the same name in a class , But the parameter list for the method is different
* rewrite : It refers to the succession relationship ( There are two classes ), Subclass to modify the original function of the parent class
rule of grammar : Method signature of subclass ( return type Method name ( parameter list ) ) Consistent with parent class
Modifiers for overriding methods : Subclass permission >= Permission of parent class
* The meaning of overloading : Is to facilitate the outside world to call the method , What kind of parameter program can find the corresponding method to execute , It reflects the flexibility of the procedure
* The significance of rewriting : Without modifying the source code , Modify and expand the functions
(OCP principle : Close for modification , Open to the outside world )

Technology
©2019-2020 Toolsou All rights reserved,
2020 The 11th National Blue Bridge Cup C/C++b Group summary ( Completion ) Review of the most complete computer network principles in history vue-cli 3 VUE Scaffold project construction ( Detailed explanation ) solve Vue+TypeScript Under development TS Don't recognize this.$refs The question of Vue Using the function of anti chattering and throttling How to use division operation in relational algebra SQL Statement representation ?copy-webpack-plugin Copy and compress files avue The use of dictionaries in English Teaching girls to learn Java: What is? Java?python Code painting Cherry Blossom -python Draw cherry tree code Specific code introduction