3 回答

TA貢獻1801條經(jīng)驗 獲得超8個贊
class G(): def m(self): print("G")class F(G): def m(self): print("F") super().m()class E(G): def m(self): print("E") super().m()class D(G): def m(self): print("D") super().m()class C(E): def m(self): print("C") super().m()class B(D, E, F): def m(self): print("B") super().m()class A(B, C): def m(self): print("A") super().m()# A^# / \# B^ C^# /| X# D^ E^ F^# \ | /# G
x = A()x.m()
class I(G):
def m(self):
print("I")
super().m()
class H():
def m(self):
print("H")
class G(H):
def m(self):
print("G")
super().m()
class F(H):
def m(self):
print("F")
super().m()
class E(H):
def m(self):
print("E")
super().m()
class D(F):
def m(self):
print("D")
super().m()
class C(E, F, G):
def m(self):
print("C")
super().m()
class B():
def m(self):
print("B")
super().m()
class A(B, C, D):
def m(self):
print("A")
super().m()
# Algorithm:
# 1. Build an inheritance graph such that the children point at the parents (you'll have to imagine the arrows are there) and
# keeping the correct left to right order. (I've marked methods that call super with ^)
# A^
# / | \
# / | \
# B^ C^ D^ I^
# / | \ / /
# / | X /
# / |/ \ /
# E^ F^ G^
# \ | /
# \ | /
# H
# (In this example, A is a child of B, so imagine an edge going FROM A TO B)
# 2. Remove all classes that aren't eventually inherited by A
# A^
# / | \
# / | \
# B^ C^ D^
# / | \ /
# / | X
# / |/ \
# E^ F^ G^
# \ | /
# \ | /
# H
# 3. For each level of the graph from bottom to top
# For each node in the level from right to left
# Remove all of the edges coming into the node except for the right-most one
# Remove all of the edges going out of the node except for the left-most one
# Level {H}
#
# A^
# / | \
# / | \
# B^ C^ D^
# / | \ /
# / | X
# / |/ \
# E^ F^ G^
# |
# |
# H
# Level {G F E}
#
# A^
# / | \
# / | \
# B^ C^ D^
# | \ /
# | X
# | | \
# E^F^ G^
# |
# |
# H
# Level {D C B}
#
# A^
# /| \
# / | \
# B^ C^ D^
# | |
# | |
# | |
# E^ F^ G^
# |
# |
# H
# Level {A}
#
# A^
# |
# |
# B^ C^ D^
# | |
# | |
# | |
# E^ F^ G^
# |
# |
# H
# The resolution order can now be determined by reading from top to bottom, left to right. A B C E D F G H
x = A()
x.m()

TA貢獻1877條經(jīng)驗 獲得超1個贊
Base3
Base1
class Base1(object): def amethod(self): print "Base1"class Base2(Base1): passclass Base3(Base1): def amethod(self): print "Base3"class Derived(Base2,Base3): passinstance = Derived()instance.amethod()class Base1: def amethod(self): print "Base1"class Base2(Base1): passclass Base3(Base1): def amethod(self): print "Base3"class Derived(Base2,Base3): passinstance = Derived()instance.amethod()
Base3Base1
添加回答
舉報