我的代码运行不起来.....(超级新手)
import random as rclass 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() Fish 的 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("太撑了,吃不下!") 谢谢!
页:
[1]