Python面向对象—类的继承

时间:2023-03-09 18:15:26
Python面向对象—类的继承

一个子类可以继承父类的所有属性,不管是父类的数据属性还是方法。

class Father(object):
num = 0 def __init__(self):
print "I'm Parent init" def foo(self):
print "I'm Parent foo" class Children(Father):
def __init__(self):
super(Children, self).__init__()
print "I'm Child init" def foo(self):
Father.foo(self) # 访问父类被覆盖的方法
print "I'm Child foo" ob = Children()
print ob.num #访问父类属性
ob.foo()
Father.foo(ob) # 访问父类被覆盖的方法

结果输出为

I'm Parent init
I'm Child init
0
I'm Parent foo
I'm Child foo
I'm Parent foo

多继承

经典类:深度优先,从左至右

新式类:广度优先,从左至右

新式类与经典类的区别在于是否有继承object基类

class P1: #(object):
def foo(self):
print "I'm P1-foo" class P2: #(object):
def foo(self):
print "I'm P2-foo"
def bar(self):
print "I'm P2-bar" class C1(P1, P2):
pass class C2(P1, P2):
def bar(self):
print "I'm C2-bar" class GC(C1, C2):
pass gc = GC()
gc.foo()
gc.bar()

它们的关系图如下

Python面向对象—类的继承

当(object)注释掉,变为经典类时,会先查找最亲的父类,结果为

I'm P1-foo    # GC -> C1 -> P1
I'm P2-bar # GC -> C1 -> P1 -> P2,如果P2没有bar()方法,则返回从C2搜索

当(object)取消注释,变为新式类时,会先查找同胞类,结果为

I'm P1-foo    # GC -> C1 -> C2 -> P1
I'm C2-bar # GC -> C1 -> C2 #找到相应方法,就不会继续继续查找父类

新式类中可以使用__mro__属性来显示查找顺序

print GC.__mro__
#结果为
#(<class '__main__.GC'>, <class '__main__.C1'>, <class '__main__.C2'>, <class '__main__.P1'>, <class '__main__.P2'>, <type 'object'>)