|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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.x -= 1
print("我的位置是:",self.x,self.y)
class Goldfish(Fish):
pass
class Carp(Fish):
pass
class Salmon(Fish):
pass
class Shark(Fish):
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print("吃货的梦想就是天天有的吃^_^")
self.hungry = False
else:
print("太撑了,吃不下!")
你的 move() 方法忘记加 self 实例对象参数了,改成这样就行:
- 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 Carp(Fish):
- pass
- class Salmon(Fish):
- pass
- class Shark(Fish):
- def __init__(self):
- self.hungry = True
-
- def eat(self):
- if self.hungry:
- print("吃货的梦想就是天天有的吃^_^")
- self.hungry = False
- else:
- print("太撑了,吃不下!")
- a = Fish()
- a.move()
- s = Shark()
- s.eat()
复制代码
|
|