小甲鱼Python视频教程第39课代码练习!
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):
#Fish.__init__(self) #调用基类的方法
super().__init__() #使用super继承父类的函数注意添加后面的括号()
self.hungry = True
def eat(self):
if self.hungry:
print("吃货的梦想就是天天有的吃!")
self.hungry = False
else:
print("太撑了,吃不下了!")