python 类报错
class Fish():def __init__(self,speed,name):
self.speed = speed
self.name = name
def say(self):
print('我得速度是%s.' % self.speed)
print('我叫%s' % self.name)
a =Fish(22,'fish')
a.say()
class Shark(Fish):
def __init__(self,speed,name,xie):
super().__init__()
self.xie=xie
def say(self):
super().say()
print('我的血量是%s' % self.xie)
b = Shark(23,'shark',23)
b.say()
报错
Traceback (most recent call last):
File "D:\Personal\桌面\新建文本文档 (5.py", line 30, in <module>
b = Shark(23,'shark',23)
File "D:\Personal\桌面\新建文本文档 (5.py", line 19, in __init__
super().__init__()
TypeError: Fish.__init__() missing 2 required positional arguments: 'speed' and 'name'
看错误信息是18行,父类初始化需要三个参数,而你的是0个 wp231957 发表于 2022-7-11 18:13
看错误信息是18行,父类初始化需要三个参数,而你的是0个
self算不算一个 class Fish():
def __init__(self,speed,name):
self.speed = speed
self.name = name
def say(self):
print('我得速度是%s.' % self.speed)
print('我叫%s' % self.name)
a = Fish(22,'fish')
a.say()
class Shark(Fish):
def __init__(self,speed,name,xie):
super().__init__(speed,name) #这里需要传入参数
self.xie=xie
def say(self):
super().say()
print('我的血量是%s' % self.xie)
b = Shark(23,'shark',23)
b.say()
shiyouroc 发表于 2022-7-11 18:15
self算不算一个
算啊,那不是提示你了吗,丟了两个参数 shiyouroc 发表于 2022-7-11 18:15
self算不算一个
注意 super() 函数是会自己传入 self 参数的,不需要自己再传了{:10_256:}
页:
[1]