Python study notes (10): object-oriented inheritance

1. Inheritance in the program

  • In the program, inheritance describes the ownership relationship between multiple classes;
  • If the attributes and methods in a class A can be reused, they can be passed to class B through inheritance, then class A is the base class, also called the parent class; class B is the derived class, also called the subclass.

The sample code is as follows:
 

# 定义父类A
class A(object):
    # 定义构造方法
    def __init__(self):
        self.num = 10
    # 实例化方法
    def print_num(self):
        print(self.num + 10)

# 定义一个子类B
class B(A):
    pass

# 实例化对象b
b = B()
print(b.num)
b.print_num()

The output is as follows:

10
20

2. Single inheritance

Single inheritance: The child class only inherits one parent class.

  • When the subclass is inherited, when defining the class, parentheses () are the name of the parent class;
  • The properties and methods of the parent class will be inherited to the child class

The sample code is as follows:

# 定义一个Master类
class Master(object):
    def __init__(self):
        # 属性
        self.formula = "古法煎饼果子配方"
    # 实例方法
    def make_cake(self):
        print("按照{}制作煎饼果子".format(self.formula))

# 定义一个子类Prentice,继承父类Master,子类可以继承父类中所有的属性和方法
class Prentice(Master):
    pass
# 实例化一个父类对象allan
allan = Master()
print(allan.formula)
allan.make_cake()

print("***" * 10)

# 实例化一个子类对象crystal
crystal = Prentice()
# 子类对象crystal可以直接使用父类的属性
print(crystal.formula)
# 子类对象crystal可以直接使用父类的方法
crystal.make_cake()

The output is as follows:

古法煎饼果子配方
按照古法煎饼果子配方制作煎饼果子
******************************
古法煎饼果子配方
按照古法煎饼果子配方制作煎饼果子

 3. Multiple inheritance

Multiple inheritance: Subclasses inherit multiple parent classes.

  • Multiple inheritance can inherit multiple parent classes, and also inherit the properties and methods of all parent classes;
  • If there are attributes and methods with the same name in multiple parent classes, the attributes and methods of the first parent class are used by default (search according to the order of the magic attribute __mro__ of the class)

The sample code is as follows:

# 定义一个Master类
class Master(object):
    def __init__(self):
        # 属性
        self.formula = "古法煎饼果子配方"
    # 实例方法
    def make_cake(self):
        print("[古法]按照{}制作煎饼果子".format(self.formula))

# 定义一个School类
class School(object):
    def __init__(self):
        # 属性
        self.formula = "现代煎饼果子配方"
    # 实例方法
    def make_cake(self):
        print("[现代]按照{}制作煎饼果子".format(self.formula))

# 定义一个子类Prentice,继承两个父类Master和School,子类可以继承父类中的所有属性和方法
# 如果父类中有同名的属性和方法,则默认使用第一个父类的属性和方法
class Prentice(Master, School):
    pass

# 实例化一个子类对象crystal,默认使用的是Master类的属性和方法
crystal = Prentice()
# 使用的是Master的属性
print(crystal.formula)
# 调用的是Master的方法
crystal.make_cake()

# 子类的魔法属性__mro__决定了属性和方法的查找顺序
print(Prentice.__mro__)

The output is as follows:

古法煎饼果子配方
[古法]按照古法煎饼果子配方制作煎饼果子
(<class '__main__.Prentice'>, <class '__main__.Master'>, <class '__main__.School'>, <class 'object'>)

4. The subclass overrides the properties and methods of the same name of the parent class

If the subclass overrides the properties and methods of the parent class with the same name, the properties and methods of the subclass will be used by default.

The sample code is as follows:

# 定义一个Master类
class Master(object):
    def __init__(self):
        # 属性
        self.formula = "古法煎饼果子配方"
    # 实例方法
    def make_cake(self):
        print("[古法]按照{}制作煎饼果子".format(self.formula))

# 定义一个School类
class School(object):
    def __init__(self):
        # 属性
        self.formula = "现代煎饼果子配方"
    # 实例方法
    def make_cake(self):
        print("[现代]按照{}制作煎饼果子".format(self.formula))

# 定义一个子类Prentice,继承两个父类Master和School
class Prentice(Master, School):
    def __init__(self):
        # 子类重写formula属性
        self.formula = "猫氏煎饼果子配方"
    # 子类重写make_cake()方法
    def make_cake(self):
        print("[猫氏]按照{}制作煎饼果子".format(self.formula))

# 实例化一个子类对象crystal
# 子类重写父类同名的属性和方法,则默认使用子类的属性和方法
crystal = Prentice()
print(crystal.formula)
crystal.make_cake()

# 子类的魔法属性__mro__决定了属性和方法的查找顺序
print(Prentice.__mro__)

The output is as follows:

猫氏煎饼果子配方
[猫氏]按照猫氏煎饼果子配方制作煎饼果子
(<class '__main__.Prentice'>, <class '__main__.Master'>, <class '__main__.School'>, <class 'object'>)

5. The subclass calls the properties and methods of the same name of the parent class

The sample code is as follows:

# 定义一个Master类
class Master(object):
    def make_cake(self):
        print("按照[古法]制作煎饼果子")

# 定义一个School类
class School(object):
    def make_cake(self):
        print("按照[现代]制作煎饼果子")

# 定义一个子类Prentice,继承了两个父类Master和School
class Prentice(Master, School):
    def make_cake(self):
        print("按照[猫氏]制作煎饼果子")

    def make_old_cake(self):
        Master.make_cake(self)

    def make_new_cake(self):
        School.make_cake(self)

# 实例化一个子类对象crystal
crystal = Prentice()
crystal.make_cake()
print("**" * 10)
crystal.make_old_cake()
print("**" * 10)
crystal.make_new_cake()

The output is as follows:

按照[猫氏]制作煎饼果子
********************
按照[古法]制作煎饼果子
********************
按照[现代]制作煎饼果子

6. The use of super()

使用super()可以逐一调用所有的父类方法,并且只执行一次。

示例代码如下:
# 定义一个Master类
class Master(object):
    def make_cake(self):
        print("按照[古法]制作煎饼果子")

# 定义一个School类,继承父类Master
class School(Master):
    def make_cake(self):
        print("按照[现代]制作煎饼果子")
        super().make_cake()

# 定义一个子类Prentice,继承了两个父类Master和School
class Prentice(School, Master):
    def make_cake(self):
        print("按照[猫氏]制作煎饼果子")
        super().make_cake()

# 实例化一个子类对象crystal
crystal = Prentice()
crystal.make_cake()

The output is as follows:

按照[猫氏]制作煎饼果子
按照[现代]制作煎饼果子
按照[古法]制作煎饼果子

 

Guess you like

Origin blog.csdn.net/weixin_44679832/article/details/114273884