Python inheritance, it is enough to read this article

foreword

When it comes to object-oriented, everyone is familiar with it. In python, everything is an object, and we use a class to represent a collection of objects with the same properties and methods. Inheritance is a way to create a new class that can use the properties of the inherited class. Let's talk about inheritance in python today.

concept of inheritance

Inheritance is used to create classes, the newly created ones are called subclasses, and the inherited ones are called parent classes. Subclasses can use parent class attributes, and inheritance describes the relationship between classes.

Why use inheritance? Because inheritance can reduce code redundancy and improve code reusability. We use inheritance a lot in our work.

type of inheritance

Inheritance in python has a total of single inheritance, multiple inheritance and multi-level inheritance.

single inheritance

Single inheritance means that a subclass inherits from only one parent class. Example:

class A():
    def __init__(self):
        self.a = 'a'

    def test_a(self):
        print("aaaa")
    

class B(A):
    def __init__(self):
        self.b = 'b'

    def test_b(self):
        self.test_a()
        print("bbbb")


obj = B()
obj.test_b()

复制代码

In the example, class B only inherits the methods of class A. Use self.test_a() in class B to call the test_a() method of class A.

multiple inheritance

Multiple inheritance means that a child class inherits from more than one parent class. Let's take an example:

class A():
    def __init__(self):
        self.a = 'a'

    def test_a(self):
        print("aaaa")
    

class B():
    def __init__(self):
        self.b = 'b'

    def test_b(self):
        print("bbbb")


class C(A, B):
    def __init__(self):
        self.c = 'c'

    def test_c(self):
        self.test_a()
        self.test_b()
复制代码

In the example, class C inherits the methods of class A and class B respectively. Multi-layer inheritance means that the parent class inherited by the subclass also inherits other classes, so I won't give an example here.

Subclass overrides parent class method

In some scenarios, the subclass inherits the properties and methods of the parent class, but the subclass has a method with the same name. In this case, the method of the subclass needs to be rewritten.

class A():
    def __init__(self):
        self.a = 'a'

    def test(self):
        print("aaaa")
    

class B(A):
    def __init__(self):
        self.b = 'b'
        super().__init__()

    def test(self):
        print("bbbb")
复制代码

In the example, class B inherits the properties and test() method of class A, but B also has a test method, so you only need to redefine a test() method in class B.

Precautions and Common Uses of Inheritance

Precautions

  • If the subclass overrides the __init__ method, the subclass will not automatically inherit the attributes in the parent class __init__. If you want to inherit the properties of the parent class, you need to use the super method, we add to the __init__ method of class B:
super(子类,self).__init__(参数0,参数1...)
父类名称.__init__(self,参数0,参数1...)
复制代码

If you inherit all the attributes of the parent class, you can use it directly super().__init__().

  • When calling the base class method, you need to add the class name prefix of the base class and bring the self parameter variable. But calling ordinary functions in a class does not need to bring the self function.

common use

Inheritance is widely used in python. For example, the source code of various modules uses inheritance. When we write code, such as configuration code and log printing, inheritance is used.

Guess you like

Origin blog.csdn.net/weixin_73136678/article/details/128421727#comments_26370745