python ,在多继承的情况下,父类都有一个相同的方法, 如何指定调用某一个父类里的方法

发布时间 2023-04-19 16:39:45作者: AngDH

 

 

 

在多继承的情况下,如果几个父类都有相同的方法,可以使用 super() 函数来指定要调用哪一个父类的方法。例如,如果要调用父类 A 的方法,可以使用以下代码:

 

class A:
    def common_method(self):
        print("This is A's common method.")


class B(A):
    def common_method(self):
        print("This is B's common method.")


class C(A):
    def common_method(self):
        print("This is C's common method.")


class D(B, C):
    def call_a_method(self):
        super(C, self).common_method()


d = D()
d.call_a_method()

 

 

 

输出结果为:“This is A's common method.”