设计一个表示动物(Animal)的类,该类包括颜色(color)属性与叫(call)方法。
再设计一个表示鱼的类,包括尾巴(tail)和颜色(color)两个属性,及叫(call)方法。
提示:让Fish类继承Animal类,重写_init_()和call方法

class Animal(object):
    def __init__(self,color):
        self.color=color
    def call(self):
        print("动物叫。。。")

class Fish(Animal):
    def __init__(self,color):
        super().__init__(color)
        self.tail=True
    def call(self):
        print("%s的鱼在吐泡泡"%self.color)
fish=Fish("蓝色")
fish.call()
animal=Animal("白色")
animal.call()

运算结果:

蓝色的鱼在吐泡泡
动物叫。。。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐