1. Multi-level inheritance

If a class has two or more parents, the inheritance relationship is called multiple inheritance.

class Master(object) :
    def __init__(self) :
        self.kongfu = "Recipe for ancient Pancake Fruit."  

    def make_cake(self) :
        print("[ancient method] according to <%s> made a pancake fruit..." % self.kongfu)


class School(object) :
    def __init__(self) :
        self.kongfu = "Modern pancake recipe"

    def make_cake(self) :
        print("[Hyundai] made a pancake according to <%s>..." % self.kongfu)


class Prentice(School, Master) :  # multiple inheritance, inheriting multiple parent classes
    def __init__(self) :
        self.kongfu = "Cat's Pancake Recipe"
        self.money = 10000  # 1 $

    def make_cake(self) :
        self.__init__() Self. Kongfu = "...."
        print("[Cat's] made a pancake according to <%s>..." % self.kongfu)


    Call the parent class method. Superclass method (self)
    def make_old_cake(self) :
        Master.__init__(self) Self. Kongfu = "old method...."
        Master.make_cake(self) The Master instance method is called


    def make_new_cake(self) :
        School.__init__(self) Self. kongfu = "modern...."
        School.make_cake(self) Call School ();

class PrenticePrentice(Prentice) : # Multilayer inheritance
    pass


pp = PrenticePrentice()
pp.make_cake() Call the instance method of the parent class
pp.make_new_cake() 
pp.make_old_cake()

print(pp.money)
Copy the code