Python object-oriented inheritance (5)

inherit

aims

  • Single inheritance
  • Multiple inheritance

Three object-oriented features

  • Encapsulation encapsulates attributes and methods into an abstract class according to responsibilities
  • Inheritance to achieve code reuse, the same code does not need to be written repeatedly
  • Polymorphic different objects call the same method, produce different execution results, increase the flexibility of the code

01. Single inheritance

1.1 The concept, grammar and characteristics of inheritance

The concept of inheritance: the child class has all the methods and properties of the parent class
Insert picture description here

1) Inherited syntax

class 类名(父类名):pass
  • The subclass inherits from the parent class and can directly enjoy the encapsulated methods in the parent class, without the need to develop it again
  • The subclass should encapsulate the unique attributes and methods of the subclass according to the responsibilities

2) Professional terms

  • Dog class is a subclass of Animal class, Animal class is the parent class of Dog class, Dog class inherits from Animal class
  • Dog class is a derived class of Animal class, Animal class is the base class of Dog class, Dog class is derived from Animal class

3) Transitivity of inheritance

Class C inherits from Class B, and Class B inherits from Class A, then Class C has all the properties and methods of Class B and Class A

The child class has all the properties and methods encapsulated in the parent class and the parent class of the parent class

Question:
Can Snarling Dog call the catch method defined in the Cat class?

Answer:
No, because there is no inheritance relationship between Snarling Dog and Cat

1.2 Method rewriting

  • The child class has all the methods and properties of the parent class
  • The subclass inherits from the parent class and can directly enjoy the encapsulated methods in the parent class, without the need to develop it again

Application scenarios

  • When the method implementation of the parent class cannot meet the needs of the subclass, the method can be overridden (override)
    Insert picture description here

There are two cases for overriding the parent class method:

1. Override the method
of the parent class 2. Extend the method of the parent class

1 Override the method of the parent class

  • If during development, the method implementation of the parent class and the method implementation of the subclass are completely different,
    you can use the override method to rewrite the method implementation of the parent class in the subclass
  • The specific implementation is equivalent to defining a method with the same name as the parent class in the subclass and implementing it
  • After rewriting, at runtime, only the rewritten method in the subclass will be called, and the method encapsulated by the parent class will no longer be called

2 Extend the parent class method

  • If during development, the method implementation of the subclass contains the method implementation of the parent class. The method implementation
    originally encapsulated by the parent class is part of the method of the subclass
  • You can use the extended method
    a, rewrite the method of the parent class in the subclass,
    b, use super() where needed. The parent class method calls the execution of the parent class method
    c, other locations in the code are for the needs of the subclass , Write subclass-specific code implementation

About super

  • Super is a special class in Python
  • super() is an object created using the super class
  • The most commonly used scenario is to call the method encapsulated in the parent class when rewriting the parent class method.

Another way to call the method of the parent class (known)
In Python 2.x, if you need to call the method of the parent class, you can also use the following method:

父类名.方法(self)

This method is currently supported in Python 3.x

This method is not recommended, because once the parent class changes, the class name of the method call location also needs to be modified

Tips
During development, do not mix the parent class name and super()

If you use the current subclass name to call the method, it will form a recursive call and an infinite loop

1.3 Private properties and private methods of the parent class

1. Subclass objects cannot directly access the private properties or private methods of the parent class within their own methods.
2. Subclass objects can indirectly access private properties or private methods through the public methods of the parent class.

  • Private properties and methods are the privacy of the object, and they are not disclosed to the outside world and cannot be directly accessed by the outside world and subclasses.

  • Private properties and methods are usually used to do some internal things.
    Example
    Insert picture description here

  • The object of B cannot directly access the __num2 attribute

  • The object of B cannot access the __num2 attribute in the demo method

  • The object of B can call the test method of the parent class in the demo method

  • Inside the test method of the parent class, you can access the __num2 attribute and the __test method

02. Multiple inheritance

concept

Subclasses can have multiple parent classes, and have all the properties and methods of the parent class

For example: children will inherit the characteristics of their father and mother
Insert picture description here

grammar

class 子类名(父类名1, 父类名2...)
   pass

2.1 Precautions for the use of multiple inheritance

statement of problem

If there are methods with the same name in different parent classes, which method in the parent class will be called when the subclass object calls the method?

Tip: When developing, you should try to avoid this easily confusing situation! - if the property or method of the same name exists between the parent class, you should avoid the use of multiple inheritance
Insert picture description here

MRO in Python-method search order (know)

  • Python provides a built-in attribute mro for classes to view the method search order
  • MRO is the method resolution order, which is mainly used to determine the calling path of methods and attributes in the case of multiple inheritance.
print(C.__mro__)

Output result

(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
  • When searching for a method, it is searched in the order from left to right according to the output result of mro
  • If a method is found in the current class, it will be executed directly without searching
  • If it is not found, it will look up whether there is a corresponding method in the next class. If it is found, it will execute directly without searching
  • If the last class is found, but the method has not been found, the program reports an error

2.2 New style and old style (classic) style

object is the base class provided by Python for all objects. It provides some built-in attributes and methods, which can be viewed using the dir function

New-style class: a class with object as the base class, recommended

Classic class: the class that does not take object as the base class, it is not recommended to use

When defining a class in Python 3.x, if no parent class is specified, object will be used as the base class of the class by default-the classes defined in Python 3.x are all new-style classes

When defining a class in Python 2.x, if no parent class is specified, object will not be used as the base class

New-style and classic classes in multiple inheritance-will affect the search order of methods

In order to ensure that the written code can run on Python 2.x and Python 3.x at the same time!
In the future, when defining a class, if there is no parent class, it is recommended to inherit from object uniformly

class 类名(object):
   pass

Guess you like

Origin blog.csdn.net/weixin_42272869/article/details/113572167