在Python中调用不同的方法
好的,我有以下内容:
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 方法。
Okay so I have the following:
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
What is the difference between invoking the is_ownable method on OwnableObject
and invoking the is_ownable method on a MobileObject.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新:根据您现在发布的代码,不可能在
MobileObject
上调用is_ownable
,因为MobileObject
似乎没有is_ownable
的定义。如果确实如此,那么差异只是
MobileObject
的定义和OwnableObject
的定义之间的差异。我更新了以下条款以说明我的意思。如果您用 Python(或任何语言,实际上)创建一个类:
然后创建一个子类:
生成的子类自动继承其超类的方法:
如您所见,
is_ownable()
和 < code>is_owned() 在两个类之间有所不同 - 在后一种情况下,由于未定义is_owned()
,因此在movable
上调用时会导致错误代码>.但是move()
在这两个类中的工作原理是相同的。Update: Based on the code you have posted now, it's impossible to call
is_ownable
on aMobileObject
because theMobileObject
doesn't appear to have a definition foris_ownable
.If it does, then the difference is simply the difference between
MobileObject
's definition andOwnableObject
's definition. I've updated the terms of the below to illustrate what I mean.If you create a class in Python (or in any language, really):
And then create a subclass:
The resulting subclass automatically inherits the methods of its superclass:
As you can see,
is_ownable()
andis_owned()
differ between the two classes -- and in the latter case, sinceis_owned()
is not defined, it causes an error when called onmovable
. Butmove()
works identically in both classes.基类中实现的所有方法都可以在子类上调用。除非您重写子类中的方法,否则将使用基本实现。
All of the methods implemented in the base class can be called on the subclass. The base implementation will be used unless you override the method in the subclass.
我认为这意味着与支持面向对象范例:
I suppose that it means the same thing as in any programming language that supports the object oriented paradigm: