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

问题描述

限时删除!!

我有两个课程,字段背景。他们看起来有点像这样:

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)."

我预计首先调用背景 init ()。要将a,b传递给Fields init (),将Field分配给a和b,然后将其中包含三个0的列表分配给字段。然后,对于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, overriding a method with a 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 ++ 虚拟方法一样。

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,重写继承的类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:44