【问题标题】:passing instance of inherited class after init (Python 2.7)在初始化后传递继承类的实例(Python 2.7)
【发布时间】:2020-01-17 10:15:14
【问题描述】:

class bclass c 继承并调用class a 的实例之后,我试图在另一个class a 中访问class b (self.variable) 的对象。我不知道如何访问在继承初始化期间创建的class b 的实例。此外,a 需要从 class k 继承。

如果我可以在class a 中访问self.variable 并进一步传递它而不显式引用class b 的实例(即,只需使用self.variable 而不是b.variable 调用它)并且没有更改我已经分配给class c 中的变量的值(因此无需重新初始化它)

感谢您的帮助,我已经在这工作了好几个小时了。

class k(object):
    def __init__(self):
        pass

class b(object):
    def __init__(self):
        self.variable = 1

class c(b):
    def __init__(self):
        b.__init__(self)# b is inherited from & initialized
        # alternative for inheritance
        #super(c,self).__init__()
        print("init(): " + str(self.variable))
    def run(self):
        self.variable = 2
        print("run(): " + str(self.variable))
        a()# here I need to pass the instance of b

class a(k):
    def __init__(self):
        k.__init__(self)# a() needs to inherit from k()
        # ERROR: cannot access variables of b()
        print("a(): " + str(self.variable))


if __name__ == "__main__":
    c().run()

【问题讨论】:

  • class a 不知道class bclass c,因此无法访问它们的属性。当你使用 python 2 时,class k 应该继承自 object 或者是一个老式的类。
  • 是的,我知道。我不知道如何才能获得a 的知识,而我无法访问 k 类(但是好的,对于这个示例,我可以更改它并稍后考虑其含义)
  • 有人能告诉我我在帖子中做错了什么吗?我真的试着做对了,然后我投了反对票。

标签: python-2.7 class inheritance multiple-inheritance init


【解决方案1】:

我想我知道该怎么做了。 如果传递实例就足够了,这将起作用:

class k(object):
    def __init__(self):
        pass

class b(object):
    def __init__(self):
        self.bb = self.bb()
    class bb:
        def __init__(self):
            self.variable = 1

class c(b):
    def __init__(self):
        b.__init__(self)# b is inherited from & initialized
        # alternative for inheritance
        #super(c,self).__init__()
        print("init(): " + str(self.bb.variable))
    def run(self):
        self.bb.variable = 99
        print("run(): " + str(self.bb.variable))
        a(self)# pass instance of b

class a(k):
    def __init__(self, bInstance):
        k.__init__(self)# a() needs to inherit from k()
        print("a(): " + str(bInstance.bb.variable))




if __name__ == "__main__":
    c().run()

据我了解,如果我想通过 self 到达那里,我需要 class a 继承 class cclass c 继承并初始化 class b,最后的调用只是 a() (它做了我需要它做的事情,因为 a 继承了我需要的一切):

class k(object):
    def __init__(self):
        pass

class b(object):
    def __init__(self):
        self.bb = self.bb()
    class bb:
        def __init__(self):
            self.variable = 1

class c(b):
    def __init__(self):
        b.__init__(self)# b is inherited from & initialized
        # alternative for inheritance
        #super(c,self).__init__()
        print("init(): " + str(self.bb.variable))
    def run(self):
        self.bb.variable = 99
        print("run(): " + str(self.bb.variable))


class a(k,c):
    def __init__(self):
        k.__init__(self)# a() needs to inherit from k()
        c.__init__(self)
        print("a(): " + str(self.bb.variable))

        self.run()
        print("a() #2: " + str(self.bb.variable))



if __name__ == "__main__":
    a()

【讨论】:

    猜你喜欢
    • 2017-10-21
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 2018-06-26
    相关资源
    最近更新 更多