【发布时间】:2016-03-07 18:09:11
【问题描述】:
我在多重继承中遇到错误。由于我是 python 新手,所以我不明白为什么我不能这样做。
class A(object):
def say_hello(self):
print 'A says hello'
class B(A):
def say_hello(self):
super(B, self).say_hello()
print 'B says hello'
class C(A):
def say_hello(self):
super(C, self).say_hello()
print 'C says hello'
class D(A, B):
def say_hello(self):
super(D, self).say_hello()
print 'D says hello'
DD = D()
DD.say_hello()
我收到错误:- 无法创建一致的方法解决方案。为什么?
【问题讨论】:
-
不会导致这个问题,但惯例是使用 self 作为第一个 arg 以在类方法中实例化 tnods 和 cls。
-
如果只删除 B 中的 super 调用会发生什么?请注意,当你到达 B 时,不知道这是否重要。
标签: python multiple-inheritance