Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在python中从继承类中获取变量?得到了类似的问题,但没有得到正确的回答_Python 3.x_Oop_Inheritance - Fatal编程技术网

Python 3.x 如何在python中从继承类中获取变量?得到了类似的问题,但没有得到正确的回答

Python 3.x 如何在python中从继承类中获取变量?得到了类似的问题,但没有得到正确的回答,python-3.x,oop,inheritance,Python 3.x,Oop,Inheritance,我正在尝试访问变量X。这是我的密码: class A: def A(self): self.DD = "aaaaaaaaaaaaaaaa" class B(A): def __init__(self): Scenario.__init__(self) def A(self): self.x = self.DD print(self.x) if __name__ == '__mai

我正在尝试访问变量
X
。这是我的密码:

class A:
    def A(self):   
        self.DD = "aaaaaaaaaaaaaaaa"
class B(A):

    def __init__(self):

        Scenario.__init__(self)
    def A(self):
        self.x = self.DD
        print(self.x)
if __name__ == '__main__':
    B = B()
    B.A()
我得到一个错误:

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-23-89f50bf48427> in <module>

     14 if __name__ == '__main__':

     15     B = B()

---> 16     B.A()

 

<ipython-input-22-d2fecfd66f95> in A(self)

     15     def A(self):

     16         self.xx = self.Business_Org

---> 17         self.x = self.DD

     18         print(self.x)

     19

 

AttributeError: 'B' object has no attribute 'DD'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在里面
14如果uuuu name uuuuuu=='\uuuuuuu main\uuuuuuuuu':
15 B=B()
--->16 B.A()
在(自我)中
15 def A(自身):
16 self.xx=self.Business\u Org
--->17 self.x=self.DD
18打印(self.x)
19
AttributeError:“B”对象没有属性“DD”
我在这里发现了一个类似的问题:。但回答不正确。
如果有人能帮助我,这将是非常值得赞赏的。

在类B中,我相信您重写了从类A继承的方法A()。这意味着self.DD永远不会被指定为A.A()
self.DD=“aaaaaaaaaa”从未被调用,因此对象从未指定DD属性。请尝试以下方法:

class A:
    def __init__(self):
        self.A()

    def A(self):   
        self.DD = "aaaaaaaaaaaaaaaa"
class B(A):

    def __init__(self):
        Scenario.__init__(self)

    def B(self):
        self.A()
        self.x = self.DD
        print(self.x)


if __name__ == '__main__':
    B = B()
    B.B() # aaaaaaaaaaaaaaaa

作为进一步的提示,您应该真正避免这种混乱的命名,因为类和方法都具有相同的名称。最好是类A()和方法A()。通常情况下,通常会看到将类大写,并将方法保持小写或小写。最美好的祝福。