简体   繁体   English

在子类的另一个方法中调用超类的方法

[英]Call the method of the super class in an other method of the subclass

I already asked about something related to the game I am developing.我已经问过与我正在开发的游戏相关的问题。 The Problem occured while the developement, but actually it has nothing to do with the game it self.问题是在开发过程中出现的,但实际上与游戏本身无关。

I have a method ('resize' in a subclass) in my Code which calls the equivalent method in it's super class ('resize' of the superclass).我的代码中有一个方法(子类中的“resize”),它调用超类中的等效方法(超类的“resize”)。

  • Expected Behaviour: Super-resize calls Super-do_rotozoom预期行为:Super-resize 调用 Super-do_rotozoom
  • What happend: Super-resize called Sub-do_rotozoom发生了什么:称为 Sub-do_rotozoom 的超级调整大小

Here is a code Example:这是一个代码示例:

Subclass:子类:

def do_rotozoom(self):
    # do rotozoom stuff of subclass
def resize(self,factor):
    super().resize(factor)
    self.do_rotozoom()

Superclass:超类:

def do_rotozoom(self):
    #do rotozoom stuff of superclass
def resize(self,factor):
    self.factor = factor
    self.do_rotozoom()

I found a workaround which involved calling super().do_rotozoom() in the Subclass method do_rotozoom() which then was called by the super().resize() .我找到了一种解决方法,它涉及在子类方法do_rotozoom()中调用super().do_rotozoom() do_rotozoom() ,然后由super().resize()调用。 I also found out, that I could in this case remove the line self.do_rotozoom() .我还发现,在这种情况下,我可以删除self.do_rotozoom()

In this case it was a pretty easy fix, but what would I do in a more complex scenario, for example, if I need to call the method do_rotozoom() with other variables in the superclass than I do in the subclass/another specific implementation?在这种情况下,这是一个非常简单的修复,但是在更复杂的情况下我会怎么做,例如,如果我需要使用超类中的其他变量调用方法do_rotozoom()而不是我在子类/另一个特定实现中所做的? In other words, how am I able to select which method I want to use in a specific context?换句话说,我如何能够选择我想在特定上下文中使用的方法?

Normaly you are only able to reach the super-methods from the subclass, but no super-methods (not of it's superclass but it's own methods) from the superclass.通常,您只能从子类访问超方法,但不能从超类访问超方法(不是超类,而是自己的方法)。

I have not found a better title... :D我还没有找到更好的标题... :D

Developers tend to prefer Composition over inheritance , it's much more manageable .开发人员倾向于更喜欢组合而不是继承,它更易于管理。

what i advise you to do is to include an instance of your superclass in you subclass and use it whenever you want to .我建议你做的是在你的子类中包含你的超类的一个实例,并在你想要的时候使用它。

The very definition of a subclass is that it inherits everything from the superclass except the methods and attributes it overrides.子类的定义是它继承了超类的所有内容,除了它覆盖的方法和属性。

A subclass can refer to its superclass and its method implementations with super() , like you already do in your example.子类可以使用super()引用其超类及其方法实现,就像您在示例中所做的那样。

Either don't override do_rotozoom , or refer to the superclass method with super().do_rotozoom() where that's the behavior you require.要么不要覆盖do_rotozoom ,要么使用super().do_rotozoom()引用超类方法,这是您需要的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM