Python Don';无法理解子类定义中此类型错误的原因
下面描述了代码和错误: 代码-Python Don';无法理解子类定义中此类型错误的原因,python,class,oop,inheritance,Python,Class,Oop,Inheritance,下面描述了代码和错误: 代码- #继承 班长: 定义初始(自我,主页): self.home=家 班子(父亲): 定义初始(自我、妻子、自行车): 超级()__初始化(自我,主页) 妻子 自行车 sonworth=儿子(“fourbhk”、“nikita”、“hayabusa”) 打印(sonworth.家,sonworth.妻子,sonworth.自行车) 错误- TypeError Traceback (most recent
#继承
班长:
定义初始(自我,主页):
self.home=家
班子(父亲):
定义初始(自我、妻子、自行车):
超级()__初始化(自我,主页)
妻子
自行车
sonworth=儿子(“fourbhk”、“nikita”、“hayabusa”)
打印(sonworth.家,sonworth.妻子,sonworth.自行车)
错误-
TypeError Traceback (most recent call last)
<ipython-input-69-b4fa474bd6de> in <module>
10 self.bike = bike
11
---> 12 sonworth = Son("fourbhk","nikita","hayabusa")
13 print(sonworth.home,sonworth.wife,sonworth.bike)
14
TypeError: __init__() takes 3 positional arguments but 4 were given
TypeError回溯(最近一次调用)
在里面
10.self.bike=自行车
11
--->12 sonworth=儿子(“fourbhk”、“nikita”、“hayabusa”)
13打印(sonworth.家,sonworth.妻子,sonworth.自行车)
14
TypeError:\uuuu init\uuuuuu()接受3个位置参数,但给出了4个
我可以看出您错过了Son
中的home
参数:
班长:
定义初始(自我,主页):
self.home=家
班子(父亲):
定义初始(自我、家庭、妻子、自行车):
超级()。\uuuuu初始\uuuuuuuuuuuuuuuuuu(主)
妻子
自行车
sonworth=儿子(“fourbhk”、“nikita”、“hayabusa”)
打印(sonworth.home,sonworth.老婆,sonworth.bike)
您忘记使用Son
中的home
参数
您也不需要使用Super()
函数,因为Father
不继承任何内容
class Father:
def __init__(self,home):
self.home = home
class Son(Father):
def __init__(self,home,wife,bike):
Father. __init__(self,home)
self.wife = wife
self.bike = bike
sonworth = Son("fourbhk","nikita","hayabusa")
print(sonworth.home,sonworth.wife,sonworth.bike)
谢谢,我的老师告诉我要使用超级函数。顺便说一句,在这种情况下,你应该使用
super()。那么您就不必在\uuuu init\uuuu()
中更改代码了。