在Python中调用不同的方法

发布于 2024-10-26 18:53:49 字数 348 浏览 3 评论 0原文

好的,我有以下内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒与心事 2024-11-02 18:53:49

更新:根据您现在发布的代码,不可能在 MobileObject 上调用 is_ownable,因为 MobileObject似乎没有 is_ownable 的定义。

如果确实如此,那么差异只是 MobileObject 的定义和 OwnableObject 的定义之间的差异。我更新了以下条款以说明我的意思。

如果您用 Python(或任何语言,实际上)创建一个类:

class MobileObject(object):
    def __init__(self, position):
        self.position = position
    def move(self, position):
        self.position = position
    def is_ownable(self):
        return False

然后创建一个子类:

class OwnableObject(MobileObject):
    def __init__(self, position, owner=None):
        MobileObject.__init__(self, position)
        self.owner = owner
    def is_ownable(self):
        return True
    def is_owned(self):
        return self.owner

生成的子类自动继承其超类的方法:

movable = MobileObject()
movable.is_ownable()       # returns False
movable.move(new_position) # moves movable
movable.is_owned()         # causes an error

ownable = OwnableObject()
ownable.is_ownable()       # returns True
ownable.move(new_position) # moves ownable
movable.is_owned()         # returns owner or None

如您所见,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 a MobileObject because the MobileObject doesn't appear to have a definition for is_ownable.

If it does, then the difference is simply the difference between MobileObject's definition and OwnableObject'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):

class MobileObject(object):
    def __init__(self, position):
        self.position = position
    def move(self, position):
        self.position = position
    def is_ownable(self):
        return False

And then create a subclass:

class OwnableObject(MobileObject):
    def __init__(self, position, owner=None):
        MobileObject.__init__(self, position)
        self.owner = owner
    def is_ownable(self):
        return True
    def is_owned(self):
        return self.owner

The resulting subclass automatically inherits the methods of its superclass:

movable = MobileObject()
movable.is_ownable()       # returns False
movable.move(new_position) # moves movable
movable.is_owned()         # causes an error

ownable = OwnableObject()
ownable.is_ownable()       # returns True
ownable.move(new_position) # moves ownable
movable.is_owned()         # returns owner or None

As you can see, is_ownable() and is_owned() differ between the two classes -- and in the latter case, since is_owned() is not defined, it causes an error when called on movable. But move() works identically in both classes.

£冰雨忧蓝° 2024-11-02 18:53:49

基类中实现的所有方法都可以在子类上调用。除非您重写子类中的方法,否则将使用基本实现。

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.

锦爱 2024-11-02 18:53:49

我认为这意味着与支持面向对象范例

>>> class Base:
...     def ok(self):
...         print 'OK'
... 
>>> class SubClass(Base):
...     def oops(self):
...         print 'Oops'
... 
>>> x = SubClass()
>>> x.ok()
OK
>>> 

I suppose that it means the same thing as in any programming language that supports the object oriented paradigm:

>>> class Base:
...     def ok(self):
...         print 'OK'
... 
>>> class SubClass(Base):
...     def oops(self):
...         print 'Oops'
... 
>>> x = SubClass()
>>> x.ok()
OK
>>> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文