class FirstClass:def setdata(self,value):self.data = valuedef display(self):print(self.data)x = FirstClass()y = FirstClass()x.setdata('King Arthur')y.setdata(3.14)x.display()y.display()x.data = 'New Data'x.display()class SecondClass(FirstClass):def display(self):print('current value = "%s"' % (self.data))z = SecondClass()z.setdata(43)z.display()class ThirdClass(FirstClass):def _init_(self,value):self.data = valuedef _add_(self,other):return ThirdClass(self.data + other)def _str_(self):return '[ThirdClass: %s]' % self.datadef mul(self,other):self.data *= otherb = ThirdClass('abc')b.display()我是照着书上敲的代码页报以下错:b = ThirdClass('abc')TypeError: object.__new__() takes no parameters
3 回答
慕码人2483693
TA贡献1860条经验 获得超9个赞
python类的初始化函数是__init__,有两个“_”,你只有1个,所以python认为你的ThirdClass没有定义初始化函数,所以它就给你调用了默认的不接受参数的初始化函数,所以它才告诉你ThirdClass的不接受有参数的初始化。
添加回答
举报
0/150
提交
取消