|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import random as r
- class Fish:
- def __init__(self):
- self.x = r.randint(0,10)
- self.y = r.randint(0,10)
- def move(self):
- self.x -= 1
- print("我的位置:",self.x, self.y)
- class Goldfish(Fish):
- pass
- class Shark(Fish):
- def __init__(self):
- super().__init__()
- self.hungry == True
- def eat(self):
- if self.hungry:
- print("Eat u!!")
- self.hungry == False
- else:
- print("Im full.")
复制代码
上面的Shark类在实例化的时候老是报错,我就是按照老师的例子打的啊,很奇怪
>>> s = Shark()
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
s = Shark()
File "C:/Users/Matthew/AppData/Local/Programs/Python/Python36/test/fish_class.py", line 17, in __init__
self.hungry == True
AttributeError: 'Shark' object has no attribute 'hungry'
>>>
|
|