在Python 3.7.2的子类中无法从两个不同的父类调用这两个函数
我尝试调用默认构造函数以及用户定义的函数 它只是从参数列表中调用第一类函数。调用(Parent1,Parent2)时会调用Parent1函数,调用(Parent2,Parent1)时会调用Parent2函数。
class Parent1():
def fun1(self):
print("Fun1 from Parent1")
def fun2(self):
print("Fun2 from Parent1")
class Child1(Parent1):
def fun2(self):
print("Fun2 from Child1")
obj1 = Child1()
obj1.fun1()
obj1.fun2()
print("========Block========")
class Parent2():
def fun1(self):
print("Fun1 from Parent2")
class Child2(Parent1, Parent2):
def fun1(self):
super().fun1()
print("Fun1 from Child2")
obj2 = Child2()
obj2.fun1()
print("========Block========")
class Child3(Parent2, Parent1):
def fun1(self):
super().fun1()
print("Fun1 from Child3")
obj3 = Child3()
obj3.fun1()
print("========Block========")
class First(object):
def __init__(self):
super(First, self).__init__()
print("First")
class Second(object):
def __init__(self):
super(Second, self).__init__()
print("Second")
class Third(object):
def __init__(self):
super(Third, self).__init__()
print("Third")
Third()
答案 0 :(得分:0)
class Parent1():
def fun1(self):
print("Fun1 from Parent1")
def fun2(self):
print("Fun2 from Parent1")
class Child1(Parent1):
def fun1(self):
Parent1.fun1(self)
print("Fun2 from Child1")
class Parent2():
def fun1(self):
print("Fun1 from Parent2")
class Child2(Parent1, Parent2):
def fun1(self):
Parent1.fun1(self)
Parent2.fun1(self)
print("Fun1 from Child2")
print("========Block========")
obj1 = Child1()
obj1.fun1()
obj1.fun2()
print("========Block========")
obj2 = Child2()
obj2.fun1()
print("========Block========")
o / p
========Block========
Fun1 from Parent1
Fun2 from Child1
Fun2 from Parent1
========Block========
Fun1 from Parent1
Fun1 from Parent2
Fun1 from Child2
========Block========