#编写动物类Animal,初始化一个type属性表示动物种类,定义一个叫声函数cry,函数暂不执行任何操作
class Animal(object):
def __init__(self,a,b):
self.type = a
self.cry = b
def info(self):
s ="我是{},我这样叫{}"
print(s.format(self.type,self.cry))
s1 = Animal("我是giao哥","1giaowoliegiaogaio")
s1.info()
#编写鸟类Bird,猫类Cat,狗类Dog,它们都继承Animal类,初始化种类分别为bird、cat、dog
class Bird(Animal):
def __init__(self):
super().__init__('bird','jiji')#相当于在上面的函数s1 =Animmal里加入进去
class Cat(Animal):
def __init__(self):
super().__init__('cat','meow')
class Dog(Animal):
def __init__(self):
super().__init__('asfas','asdfas')
#分别创建一个鸟,猫,狗对象,调用它们的介绍函数
a = Bird()
a.info()
b = Cat()
b.info()
c = Dog()
c.info()
#编写燕子类,继承鸟类,添加一个属性:子类型(subtype)。燕子的子类型为'swallow'。
#重写燕子的介绍函数,打印:我是xxx,属于yyy,我的叫声是zzz,其中xxx是子类型,yyy是类型,zzz是叫声
class swallow(Bird):
def __init__(self):
self.subtybe = 'swallow'#这个是新加入的类型:swalow燕子
super().__init__()#继承bird的类型和叫声,()括号内不写东西代表继承全部
def bili(self):
print("我是"+self.subtybe+"属于"+self.type+"我的叫声是"+self.cry)
s = swallow()
s.bili()
89十11编写动物类Animal初始化一个type属性表示动物种类,定义一个叫声函数cry,函数暂不执行任何操作九、编写鸟类Bird,猫类Cat,狗类Dog,它们都继承Animal类,初始化种类分别为
最新推荐文章于 2022-08-31 17:05:19 发布