我是python新手,在python中学习OOP时,我遇到了如下错误
AttributeError: 'Dog' object has no attribute 'sound'
对于以下代码
^{pr2}$现在,我的另一个疑问与继承有关,我在继承中遇到了如下错误:
AttributeError: 'Bulldog' object has no attribute 'speed'
以下代码:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def description(self):
return print(f"name is {self.name} and age is {self.age}")
class Bulldog(Dog):
def run(self, speed):
return print(f"The speed of dog is {self.speed}")
tommy = Bulldog("tommy",10)
tommy.description()
tommy.run(5)
我相信你需要摆脱自我。当试图返回打印时,因为这些是作为参数传递的,而不是在对象本身中标识的。在
这对于速度和声音都是一样的,注意我还更改了一些关于print语句如何工作的格式
相关问题 更多 >
编程相关推荐