Python,覆盖继承的类方法 [英] Python, Overriding an inherited class method

查看:26
本文介绍了Python,覆盖继承的类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,FieldBackground.它们看起来有点像这样:

I have two classes, Field and Background. They look a little bit like this:

class Field( object ):
    def __init__( self, a, b ):
        self.a = a
        self.b = b
        self.field = self.buildField()

    def buildField( self ):
        field = [0,0,0]
        return field

class Background( Field ):
    def __init__( self, a, b, c ):
        super(Background, self).__init__( a, b )
        self.field = self.buildField( c )

    def buildField( self, c ):
        field = [c]
        return field

a, b, c = 0, 1, 2
background = Background( a, b, c )

此错误指向 Field 的 buildField():

This error is pointing to Field's buildField():

"TypeError: buildField() takes exactly 2 arguments (1 given)."

我希望首先调用 Background init().将 "a, b" 传递给 Fields init(),Field 分配 a 和 b,然后将其中包含三个 0 的列表分配给 field.然后让 Background 的 init() 继续,然后调用它自己的 buildField() 并用包含 c 的列表覆盖 self.field.

I expected Background init() to be called first. To pass "a, b" to Fields init(), Field to assign a and b then to assign a list with three 0's in it to field. Then for Background's init() to continue, to then call its own buildField() and override self.field with a list containing c.

我似乎并不完全理解 super(),但是在查看网络上和这里的类似继承问题后,我无法找到解决我的问题的方法.

It seems I don't fully understand super(), however i was unable to find a solution to my issue after looking at similar inheritance problems on the web and around here.

我期望像 c++ 这样的行为,其中类可以覆盖继承的方法.我怎样才能做到这一点或类似的事情.

I expected behavior like c++ where a class can override a method that was inherited. How can i achieve this or something similar.

我发现与此相关的大多数问题是人们使用双下划线.我使用 super 继承的经验是使用继承的类 init() 将不同的变量传递给超类.没有涉及覆盖任何内容.

Most issues I found related to this were people using double underscores. My experience with inheritance with super is using the inherited class init() to just pass different variables to the super class. Nothing involving overwriting anything.

推荐答案

从 C++ 的角度来看,这里可能有两个误解.

Coming from a C++ perspective, there might be two misconceptions here.

首先,具有相同名称和不同签名的方法不会像在 C++ 中那样重载它.如果您的 Background 对象之一尝试不带参数调用 buildField,则不会调用 Field 中的原始版本——它已完全隐藏.

First, a method with the same name and different signature does not overload it like in C++. If one of your Background objects tries to call buildField with no arguments, the original version from Field will not be called -- it has been completely hidden.

第二个问题是如果超类中定义的方法调用buildField,会调用子类版本.在python中,所有方法都是动态绑定的,就像C++的virtual方法一样.

The second issue is that if a method defined in the superclass calls buildField, the subclass version will be called. In python, all methods are bound dynamically, like a C++ virtual method.

Field 的 __init__ 期望处理一个具有不带参数的 buildField 方法的对象.您将该方法用于具有带一个参数的 buildField 方法的对象.

Field's __init__ expected to be dealing with an object that had a buildField method taking no arguments. You used the method with an object that has a buildField method taking one argument.

super 的特点是它不会改变对象的类型,所以你不应该改变超类方法可能调用的任何方法的签名.

The thing with super is that it doesnt change the type of the object, so you shouldn't change the signature of any methods that the superclass' methods might call.

这篇关于Python,覆盖继承的类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆