深度学习-推荐系统-CV-NLP人工智能/模式识别/机器学习精华专题大数据,机器学习,人工智能

Python札记35_多继承

2019-07-07  本文已影响0人  皮皮大

在上篇札记Python札记34中主要是讨论继承单继承方面的知识,本篇札记中主要是学习多继承的特点。

一个类是可以继承自多个父类的

class Person:   # 定义一个父类
    
    def __init__(self, name):  # 初始化函数
        self.name = name   #实例self的name属性
        
    def age(self, n):   #  创建age方法
        return "The age of Peter is {}".format(n)
    
class Boy:    # 定义Boy类
    heigth = 180   # 定义类的属性heigth,下面直接调用
    def color(self):
        return "The boy is  yellow"
        
class TallBoy(Person, Boy):
    pass
    
if __name__ == "__main__":
    boy = TallBoy("Peter")
    print(boy.name)
    print(boy.age(25))
    print(boy.heigth)
    print(boy.color())

结果:

Peter
The age of Peter is 25
180
The boy is  yellow

继承的顺序问题
在Python2的新式类和Python3中,继承顺序都是“广度优先”,和算法MRO(Method Resolution Order)相关。

class K1:
    def foo(self):
        print("K1-foo")
    
class K2:
    
    def foo(self):
        print("K2-foo")
        
    def bar(self):
        print("K2-bar")
        
class J1(K1, K2):
    pass

class J2(K1, K2):
    def bar(self):
        print("J2-bar")
        
class C(J1, J2):
    pass

if __name__ == "__main__":
    print(C.__mro__)
    m = C()
    m.foo()
    m.bar()

结果为

(<class '__main__.C'>, <class '__main__.J1'>, <class '__main__.J2'>, <class '__main__.K1'>, <class '__main__.K2'>, <class 'object'>)
K1-foo
J2-bar

image.png

如果有继承关系如上图所示,应该怎么写出Python代码?笔者写法如下:

class D:
    def foo(self):
        print("foo in D")
        
    def bar(self):
        print("bar in D")
        
class E:
    def bar(self):
        print("bar in E")
        
class F:
    def foo(self):
        print("foo in F")
        
class B(D, E):
    pass

class C(D, F):
    def age(self, n):
        print("His age is {}".format(n))
        
class A(B, C):
    pass

a = A()
print(A.__mro__)
a.foo()     # 先找B有没有foo,再找C;都没有,再找D,有foo
a.bar()
a.age(20)
(<class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.D'>, <class '__main__.E'>, <class '__main__.F'>, <class 'object'>)
foo in D
bar in D
His age is 20

对比代码:

class D:
    def foo(self):
        print("foo in D")
        
    def bar(self):
        print("bar in D")
        
class E:
    def bar(self):
        print("bar in E")
        
class F:
    def foo(self):
        print("foo in F")
        
class B(D, E):
    pass

class C(F, D):     # 代码中修改地方:将F和D类交换位置,再看结果的差异
    def age(self, n):
        print("His age is {}".format(n))
        
class A(B, C):
    pass

a = A()
print(A.__mro__)
a.foo()
a.bar()
a.age(20)
(<class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.F'>, <class '__main__.D'>, <class '__main__.E'>, <class 'object'>)
foo in F   # 差别就是在这里:因为C类的继承改成了F,D。Python解释器在进行搜索的时候,先找类F中是否有foo方法
bar in D
His age is 20
上一篇 下一篇

猜你喜欢

热点阅读