标签:
1.面向对象的三大特性:
封装、继承、多态(在Python中使用少)
__author__ = ‘Alex‘ #coding:utf-8 class F1: def show(self): print ("F1 show") def foo(self): print (self.name) class F2(F1): def __init__(self,name): self.name = name def run(self): print ("F2 run")
obj2 = F2(‘alex‘)
obj2.foo()
执行结果:
alex
2.python继承的两种方式:
假设如上的架构,我们定义各个类之间的关系:
class C5(): def f5(self): print ("C5") class C3(C5): def f3(self): print ("C3") class C4(): def f3y(self): print ("C4") class C1(C3): def f2(self): print ("C1") class C2(C4): def f2(self): print ("C2") class C0(C1,C2): def f0(self): print ("C0") obj = C0() obj.f2()
执行方式1:C1,C2都有相同的f2方法,执行打印“C1”,向上查找。
__author__ = ‘Alex‘ class C5(): def f5(self): print ("C5") class C3(C5): def f2(self): print ("C3-F2") class C4(): def f3y(self): print ("C4") class C1(C3): def f1(self): print ("C1") class C2(C4): def f2(self): print ("C2-F2") class C0(C1,C2): def f0(self): print ("C0") obj = C0() obj.f2()
执行方式2:C1没有f2方法,C2和C3都有f2方法,打印C0().f2,可以查找到"C3-F2",向上查找
__author__ = ‘Alex‘ class C5(): def f2(self): print ("C5-F2") class C3(C5): def f3(self): print ("C3-F2") class C4(): def f3y(self): print ("C4") class C1(C3): def f1(self): print ("C1") class C2(C4): def f2(self): print ("C2-F2") class C0(C1,C2): def f0(self): print ("C0") obj = C0() obj.f2()
执行方法3:假如C3也没有f2方法,C5有,那么打印"C5-F2",如果C5也没有,那么才打印C2的“C2-F2”
3.假如他们有共同的父类
执行方法1:C1,C2都有f2方法,调用C0().f2()方法,打印“C1-F2”
__author__ = ‘Alex‘ class C5(): def f5(self): print ("C5-F2") class C3(C5): def f3(self): print ("C3-F2") class C4(C5): def f3y(self): print ("C4") class C1(C3): def f2(self): print ("C1-F2") class C2(C4): def f2(self): print ("C2-F2") class C0(C1,C2): def f0(self): print ("C0") obj = C0() obj.f2()
执行方法2:C1没有f2方法,C2和C3都有f2方法,执行结果:“C3-F2”
__author__ = ‘Alex‘ class C5(): def f5(self): print ("C5-F2") class C3(C5): def f2(self): print ("C3-F2") class C4(C5): def f3y(self): print ("C4") class C1(C3): def f3(self): print ("C1-F2") class C2(C4): def f2(self): print ("C2-F2") class C0(C1,C2): def f0(self): print ("C0") obj = C0() obj.f2()
执行方法3:C1,C3都没有f2方法,C2和C5有f2方法,执行结果:“C2-F2”
__author__ = ‘Alex‘ class C5(): def f2(self): print ("C5-F2") class C3(C5): def f3(self): print ("C3-F2") class C4(C5): def f3y(self): print ("C4") class C1(C3): def f3(self): print ("C1-F2") class C2(C4): def f2(self): print ("C2-F2") class C0(C1,C2): def f0(self): print ("C0") obj = C0() obj.f2()
执行方法4:C1,C2,C3都没有f2方法,C4有f2方法,执行结果:“C4-F2”
__author__ = ‘Alex‘ class C5(): def f2(self): print ("C5-F2") class C3(C5): def f3(self): print ("C3-F2") class C4(C5): def f2(self): print ("C4-F2") class C1(C3): def f3(self): print ("C1-F2") class C2(C4): def f7(self): print ("C2-F2") class C0(C1,C2): def f0(self): print ("C0") obj = C0() obj.f2()
执行方法5:往上在扩展一层,C2和C5有f2,执行结果“C5-F2”
执行结果6:往上在扩展一层,C1,C3,C5都没有f2,C2,C7有f2,执行结果:"C2-F2"
结论:如果他们没有相同的父类,从多继承的左边往上开始查找,一直查到底,没有的话再查右边。如果他们有相同的父类,从多继承的左边开始一直找到根类,但不查找根类的方法,如果都没有,再从右边开始。
__author__ = ‘Alex‘
class C7():
def f7(self):
print ("C7-F2")
class C6(C7):
def f6(self):
print ("C6-F2")
class C5(C7):
def f2(self):
print ("C5-F2")
class C3(C5):
def f3(self):
print ("C3-F2")
class C4(C6):
def f4(self):
print ("C4-F2")
class C1(C3):
def f3(self):
print ("C1-F2")
class C2(C4):
def f2(self):
print ("C2-F2")
class C0(C1,C2):
def f0(self):
print ("C0")
obj = C0()
obj.f2()
标签:
原文地址:http://www.cnblogs.com/python-study/p/5727624.html