简体   繁体   English

是否可以从子类更新超类中初始化的对象?

[英]Is it possible to update an object initialized in a superclass from a subclass?

So lets say I have a class: 所以我想说我有一节课:

class SuperClass():
    def __init__(self, number):
       self._inputnumber = number
       self._initallist = []

And then I want to build subclasses of this class which have methods that can add items to the initialized list, read items from it, or remove items from it. 然后我想构建这个类的子类,它的方法可以将项添加到初始化列表,从中读取项或从中删除项。 For example. 例如。

class SubClass(SuperClass):
     def __init__(self, number, current_line, new_line): 
        self._number = number
        self._thisline = current_line
        self._gohere = new_line
     def execute(self):
        (SuperClass._initallist).append(self._thisline + 1)

This is a bit of a rough example of what I am trying to do. 这是我想要做的一个粗略的例子。 I want to be able to have my initial list available for a couple of classes so that they can both act upon it as shown above. 我希望能够将我的初始列表提供给几个类,以便它们可以如上所示对其进行操作。 What ends up happening in my case, though, is that I get an AttributeError saying that my SuperClass doesn't have the named attribute. 然而,在我的情况下最终发生的是,我得到一个AttributeError,说我的SuperClass没有命名属性。

AttributeError: type object 'SuperClass' has no attribute '_initiallist'

Any tips on how to do this? 关于如何做到这一点的任何提示? Is it even possible or do I have to do something else to achieve this result? 它甚至可能还是我必须做其他事情来实现这个结果?

_initallist is an instance attribute, you can not access it by Super._initallist . _initallist是一个实例属性,您无法通过Super._initallist访问它。

I think what you want to do is below. 我想你想做的就是下面。 You need to init SuperClass in the SubClass . 您需要在SubClass初始化SuperClass And I think if this is a right is-one relation, there need to be a number in SubClass . 我认为如果这是一个正确的一对一关系,那么SubClass需要有一个number

class SuperClass():
    def __init__(self, number):
       self._inputnumber = number
       self._initallist = []

class SubClass(SuperClass):
     def __init__(self, number, current_line, new_line):
        SuperClass.__init__(self, number) 
        self._thisline = current_line
        self._gohere = new_line
     def execute(self):
        self._initallist.append(self._thisline + 1)

只做self._initiallist.append(self._thisline + 1)

"I want to build subclasses of this class which have methods that can add items to the initialized list,..." “我想构建这个类的子类,它具有可以将项添加到初始化列表的方法,......”

Are you saying you want the subclasses to inherit the instance attribute _initiallist and provide methods to act upon it? 您是否希望子类继承实例属性 _initiallist并提供对其进行操作的方法? If this is the case then instances of the subclasses will each have an _initiallist attribute but each _initiallist for each instance of the class will be a different list. 如果是这种情况,那么子类的实例将各自具有_initiallist属性, 每个类的实例的每个_initiallist将是不同的列表。

In that case the solution is as zhangyangyu described. 在那种情况下,解决方案就像张扬宇所描述的那样。


"I want to be able to have my initial list available for a couple of classes so that they can both act upon it as shown above." “我希望能够将我的初始列表提供给几个类,这样他们就可以按照上面的说明对其进行操作。”

Based on the title, I could interpret this as: I want the _initiallist attribute of some instance of the superclass to be available for instances of a couple of subclasses so they can all act upon the same exact list. 基于标题,我可以将其解释为:我希望超类的某个实例_initiallist属性可用于几个子类的实例 ,因此它们都可以在相同的精确列表上运行。

You could do that if _initiallist is a class attribute, or you could pass an instance of the superclass as an __init__ argument when instantiating the subclass, as far as I know currently. 如果_initiallist是一个类属性,你可以这样做,或者你可以在实例_initiallist类时_initiallist类的实例作为__init__参数传递,据我所知。

Also, this would mean that the two classes in your example do not posses an is a relationship, but rather, your superclass is really a sharedobject (AFAIK). 此外,这将意味着,这两个班在你的例子并不posses的是有关系的,而是超类是一个真正的共享对象(据我所知)。

If you make it a class attribute then you lose some flexibility because then different instances of the superclass will all have an attribute _initiallist that points to the same exact list. 如果你使它成为一个类属性,那么你会失去一些灵活性,因为那时超类的不同实例都会有一个属性_initiallist ,它指向同一个完全列表。 Where as if you keep it as an instance attribute and pass an instance of the superclass to the subclass as an argument (as a shared object), then you can still achieve what you want to do and still have the flexibility to create multiple instances of your shared object. 如果你将它作为一个实例属性保存并将超类的实例作为参数(作为共享对象)传递给子类,那么你仍然可以实现你想要做的事情,并且仍然可以灵活地创建多个实例你的共享对象。

class SharedObject(object):

    def __init__(self, number):
        self._inputnumber = number
        self._initiallist = []


class Client(object):

    def __init__(self, obj, current_line, new_line):
        self._sharedobj = obj
        self._thisline = current_line
        self._gohere = new_line

    def execute(self):
        self._sharedobj._initiallist.append(self._thisline + 1)

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

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