class GrandParent():
def __init__(self, a, b):
self._a = a
self._b = b
def blah(self):
return "GP:" + self._a + self._b
class Parent2(GrandParent):
def __init__(self, a, b, c):
self._a = b
self._b = a
self._c = self.blah()
class Child2(Parent2):
def __init__(self, a, b, c, d):
Parent2.__init__(self, a, b, c)
def blah(self):
return ("C2: " + self._a + self._b
+ self._c + self._d)
c2 = Child2("A", "B", "C", "D")
这是我应该追踪的代码。我创建了一个对象c2
的Child2
。我进去了。我进去了。我初始化self._a
,self._b
我的问题是self._c
Parent2
没有blah()
方法,因此我希望它从GrandParent.blah()
获取self._c
,但它会转到Child2.blah()
。为什么会发生这种情况
因为
self
是Child2
的直接实例,所以self.blah()
必须等价于Child2.blah(self)
。代码恰好位于调用之前Child2
的父对象的方法中是不相关的相关问题 更多 >
编程相关推荐