面向对象

1.类名.__mro__  可打印参看函数的所有父类

2.super()方法

 

class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')


class School(Master):
    def __init__(self):
        self.kongfu = '[黑马煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

        # 2.1 super()带参数写法
        # super(School, self).__init__()
        # super(School, self).make_cake()

        # 2.2 无参数的super
        super().__init__()
        super().make_cake()


class Prentice(School):
    def __init__(self):
        self.kongfu = '[独创煎饼果子技术]'

    def make_cake(self):
        self.__init__()
        print(f'运用{self.kongfu}制作煎饼果子')

    # 子类调用父类的同名方法和属性:把父类的同名属性和方法再次封装
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)

    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)

    # 需求:一次性调用父类School Master的方法
    def make_old_cake(self):
        # 方法一:如果定义的类名修改,这里也要修改,麻烦; 代码量庞大,冗余
        # School.__init__(self)
        # School.make_cake(self)
        # Master.__init__(self)
        # Master.make_cake(self)

        # 方法二:super()
        # 2.1 super(当前类名, self).函数()
        # super(Prentice, self).__init__()
        # super(Prentice, self).make_cake()

        # 2.2 无参数super
        super().__init__()
        super().make_cake()


daqiu = Prentice()

daqiu.make_old_cake()

 

上一篇:c/c++贪心算法求月饼利润


下一篇:1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts (M)