Python 继承问题:AttributeError:';向量';对象没有属性'_向量i';

Python 继承问题:AttributeError:';向量';对象没有属性'_向量i';,python,inheritance,Python,Inheritance,我收到一个错误,说“在添加返回向量((self.\uu I+o.\uu I),(self.\uu j+o.\uj),(self.\uu k+o.\uk))” AttributeError:“Vector”对象没有属性“\u Vector\u i” 我想用一个属性“k”创建一个新的子类,并将2d add方法更改为3d add方法。这个继承哪里出错了?在Vector的\uuuu add\uuuu方法中,您访问属性\uu I和\uu j,而从未初始化它们。这就是为什么会出现错误。@chepner即使没

我收到一个错误,说“在添加返回向量((self.\uu I+o.\uu I),(self.\uu j+o.\uj),(self.\uu k+o.\uk))” AttributeError:“Vector”对象没有属性“\u Vector\u i”


我想用一个属性“k”创建一个新的子类,并将2d add方法更改为3d add方法。这个继承哪里出错了?

Vector
\uuuu add\uuuu
方法中,您访问属性
\uu I
\uu j
,而从未初始化它们。这就是为什么会出现错误。

@chepner即使没有名称混乱,代码也只是有缺陷——它访问的属性从未在构造函数中设置过。事实上,这就是为什么我只发布了一个指向该问题的链接,而不是将其作为重复项关闭。我实际上发现了问题。这是因为我将self.real和self.imag设置为私有财产。如果我想在子类中使用它们,我需要在父类中使用class方法来调用该属性。
    class ComplexNumber:
        # setting a Debug value and will be used to ask user if they want to enter degub mode
        Debug = None
        # initiating variables, self consists of real, imag, and complex. 
        def __init__(self, real,imag):
        #The prefix __ is added to make the variable private
            self.__real = real
            self.__imag = imag
        # Overload '+','-' and '*' to make them compatible for 2 by 2 matrix operation
        def __add__(self, o): 
            return ComplexNumber((self.__real + o.__real),(self.__imag + o.__imag))
        def __sub__(self, o): 
            return ComplexNumber((self.__real - o.__real),(self.__imag - o.__imag))
        def __mul__(self,o):
            return ComplexNumber((self.__real * o.__real - self.__imag * o.__imag),\
        (self.__imag * o.__real + self.__real * o.__imag))

    # Create a child class from ComplexNumber
    class Vector(ComplexNumber):
    #inherit the property of real and image as i and j
        def __init__(self, i,j,k):
           super().__init__(i, j)
           self.k = k
        def __add__(self, o): 
           return Vector((self.__i + o.__i),(self.__j + o.__j),(self.__k + o.__k))

    A = Vector(1,0,3)
    B = Vector(1,0,3)
    print(A+B)