python class inheritance and composition

class inheritance

The inheritance syntax is class derived class name (base class name): the base class name is written in parentheses, and the base class is specified in the tuple when the subclass is defined. That is, the parentheses in the definition of the subclass are a tuple, which is the class name of the base class.

class A():
    pass

class B(A):#class B inherits class A
    pass
Example

Subclasses need to use the format of BaseClassName.F(self) to call the method of the base class, and the format of calling the method of this class is the format of self.F(self).

class A():
    def print1(self):
        print('a... ...')

    def print2(self):
        print('aa... ...')


class B(A):
    def print1(self):
        print('b... ...')

    def print2(self):
        print('bb... ...')

    def print3(self):
        self.print1()  # call a method in a class
        A.print1(self)  # call base class method

b = B()
b.print3()
Example

Subclasses can inherit all public variables and methods of the base class, but cannot inherit private variables and methods.

class A():
    name = 'abc'
    __score = 100

class B(A):
    def print1(self):
        print(self.name)
        # print(self.score)  # Private members of base classes are not inherited

b = B()
b.print1()
Example

You can choose to inherit multiple base classes in parentheses, separated by commas, this kind of inheritance is called multiple inheritance

class A():
    namea = 'a'

class B():
    nameb = 'b'

class C(A, B):
    pass

c = C()
print(c.namea, c.nameb)
Example

If both the subclass and the base class have the __init__() constructor, the constructor will be overridden. Calling the base class method in the subclass follows the above function principle of calling the base class from the subclass, in the format BaseClassName.F(self) . You can also use super.__init__() to call the constructor of the base class in the order of MRO resolution (but I don't understand MRO yet).

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

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

class C(A, B):
    def __init__(self):
        A.__init__(self)  # Call the constructor of the base class in the form of BaseClassName.F(self)
        B.__init__(self)
        print('c... ...')

c = C()
Example

combination of classes

Class inheritance mainly solves the vertical relationship classes, such as vehicle classes, high-speed rail, and airplane classes. The combination of classes solves horizontal relationships, such as school and students and teachers.

class Student():
    def __init__(self, num):
        self.num = num

class Teacher():
    def __init__(self, num):
        self.num = num

class School():
    def __init__(self, num1, num2):
        self.student = Student(num1)
        self.teacher = Teacher(num2)

school = School(100, 10)
print(school.student.num, school.teacher.num)
Example

References

  1. python: inheritance, composition

Posted by nitromaster on Tue, 24 May 2022 14:25:07 +0300