【发布时间】:2011-03-25 14:26:46
【问题描述】:
好的,我有以下内容:
Class OwnableObject(MobileObject):
def __init__(self, name):
MobileObject.__init__(self, name)
self.owner = None # not owned
def is_ownable(self): return True
def is_owned(self): return self.owner
在 OwnableObject 上调用 is_ownable 方法有什么区别
并在 MobileObject 上调用 is_ownable 方法。
【问题讨论】:
-
我认为这是一个通用的编程问题,而不是特定于 Python 的。您应该阅读一些有关面向对象编程的文本中的类和继承。
-
123,请参阅下面我编辑的答案。由于这里似乎没有为
MobileObject定义is_ownable,因此您不能这样做:mo = MobileObject(); mo.is_ownable()。结果将是一个错误。我的回答显示了如何做你所描述的。
标签: python inheritance subclass