Python 具有多个参数的_str__函数处的继承错误
我是python新手,正在学习多重继承。我有一个关于孩子的功能的问题。当我试图编译代码时抛出这个错误Python 具有多个参数的_str__函数处的继承错误,python,inheritance,multiple-inheritance,Python,Inheritance,Multiple Inheritance,我是python新手,正在学习多重继承。我有一个关于孩子的功能的问题。当我试图编译代码时抛出这个错误 return self.FiguraGeometrica.__str__() + self.Color.__str__() + str(self.area()) AttributeError: 'Cuadrado' object has no attribute 'FiguraGeometrica' 我的孩子班是: from figura_geometrica import FiguraGeo
return self.FiguraGeometrica.__str__() + self.Color.__str__() + str(self.area())
AttributeError: 'Cuadrado' object has no attribute 'FiguraGeometrica'
我的孩子班是:
from figura_geometrica import FiguraGeometrica
from color import Color
class Cuadrado(FiguraGeometrica, Color):
def __init__(self, lado, color):
FiguraGeometrica.__init__(self, lado, lado)
Color.__init__(self, color)
def __str__(self):
return self.FiguraGeometrica.__str__() + self.Color.__str__() + str(self.area())
def area(self):
return self.alto * self.ancho
另一类是:
class FiguraGeometrica:
def __init__(self, ancho, alto):
self.__ancho = ancho
self.__alto = alto
def __str__(self):
return "Ancho: " + str(self.__ancho) + ", alto: " + str(self.__alto)
def get_ancho(self):
return self.__ancho
def set_ancho(self, ancho):
self.__ancho = ancho
def get_alto(self):
return self.__alto
def set_alto(self, alto):
self.__alto = alto
执行cuadrado类测试的文件为:
from figura_geometrica import FiguraGeometrica
from color import Color
class Cuadrado(FiguraGeometrica, Color):
def __init__(self, lado, color):
FiguraGeometrica.__init__(self, lado, lado)
Color.__init__(self, color)
def __str__(self):
return self.FiguraGeometrica.__str__() + self.Color.__str__() + str(self.area())
def area(self):
return self.alto * self.ancho
谢谢你的支持 超类不是类实例的属性 您只需直接调用超类的方法,将self作为普通参数传递
def __str__(self):
return FiguraGeometrica.__str__(self) + Color.__str__(self) + str(self.area())
超类不是类实例的属性 您只需直接调用超类的方法,将self作为普通参数传递
def __str__(self):
return FiguraGeometrica.__str__(self) + Color.__str__(self) + str(self.area())
对非常感谢你@BarmarYes!非常感谢你@Barmar