Class inheritance

inherit:

1. inherit the parent class, you need to when you define a subclass, write the name of the parent class in brackets

2. rewritten: subclass method and the same name as the parent class when the method is called a method of rewriting

3. Development: the parent class is not a subclass called expansion

4. Some properties and methods of the parent class, the subclass inherits can be used directly

5. The method of the same name exists the parent class and subclass priority subclass using this method when the call subclass instance

Example 6. parent class can call the parent class method, the method can not be called a subclass of

7. subclass function can call the parent class function

Multiple inheritance: rarely used in practice, the interview may ask

1. subclass inherits more than one parent class

2. calls the same function, follow the principle of proximity: including initialization function

When a plurality of parent class method with the same name exists, the priority call the parent class of the same name in parentheses is the position of the front

3. inherit more than one parent class, the number of parameters and parameter name of the initialization function more parent classes need to be consistent, and if not, it may be an error when calling

For example, a parent has A parameter, not a parent, so when you call the method using the A parameter, the error will be

If not the number of parameters, parameter passing is performed when instantiating the error will

Example 1:




Example 2:

Guess you like

Origin www.cnblogs.com/come202011/p/12229683.html