Inheritance of classes
1 Son inherits the father's business
extends (inheriting the normal function of the parent class) (method)
class Father { constructor() { } money() { (100); } } class Son extends Father { } class sunzi extends Son { } var yxf = new Father; var lbw = new Son; var bb = new sunzi; (()); (()); (());
Usage of super
Used to access and call functions on the object's parent class. You can call the constructor of the parent class, or you can call the normal functions (methods) of the parent class.
class Father1 { constructor(x,y) { = x; = y; } sum() { ( + ); } } class Son1 extends Father1 { constructor(x,y){ super(x,y); } } var yxf = new Son1(1,2); ();
The principle of proximity of super keyword calls
<script> //The super keyword calls the parent class normal function class Father { say() { return 'I am a dad'; } } class Son extends Father { say() { // return 'I am the son'; ( ()); } } var yxf = new Son(); ();//Return to the result: I am my son //Principles for finding attributes or methods in inheritance: proximity principle //1. In inheritance, if you instantiate the subclass and output a method, first check whether the subclass has this method. If there is, execute the subclass first; //2. In inheritance, if there is no subclass, look for whether the parent class has it or not. If it has it, use the parent class. </script>
Subclasses inherit the parent class and extend their own methods at the same time
Note: The sub-constructor of the sub-class sub-constructor must be placed in front of this (the constructor of the parent class must be called first and then the constructor of the sub-class) The father is always the first! ! ! !
<script> class Father { constructor(x,y){ = x; = y; } sum() { ( + ); } } // Subclass inherits parent class addition and expands subtraction class Son extends Father { constructor(x,y) { //Use super to call the constructor of the parent class //super must be called before subclass this super(x,y); = x; = y; } sub() { ( - ); } } var son = new Son(1,2); (); (); </script>
<script> //The super keyword calls the parent class normal function class Father { say() { return 'I am a dad'; } } class Son extends Father { say() { // return 'I am the son'; ( ()); } } var yxf = new Son(); ();//Return to the result: I am my son //Principles for finding attributes or methods in inheritance: proximity principle //1. In inheritance, if you instantiate the subclass and output a method, first check whether the subclass has this method. If there is, execute the subclass first; //2. In inheritance, if there is no subclass, look for whether the parent class has it or not. If it has it, use the parent class. </script>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.