27_python实操案例十三

 

 

任务一:

复制代码
class Instrument():
    def make_sound(self):
        pass

class Erhu(Instrument):
    def make_sound(self):
        print('二胡在演奏')
class Piano(Instrument):
    def make_sound(self):
        print('钢琴在演奏')

class Violin(Instrument):
    def make_sound(self):
        print('小提琴在演奏')

#   演奏的函数
def play(instrument):
    instrument.make_sound()


class Bird():
    def make_sound(self):
        print('小鸟在唱歌')

if __name__ == '__main__':
    play(Erhu())
    play(Piano())
    play(Violin())
    play(Bird())
复制代码

 

任务二:

复制代码
class Car(object):
    def __init__(self, type, no ):
        self.type = type
        self.no = no
    def start(self):
        pass

    def stop(self):
        pass
class Taxi(Car):
    def __init__(self, type, no, company):
        super().__init__(type, no)
        self.company = company
    def start(self):
        print('乘客您好!')
        print(f'我是{self.company}出租车公司的,我的车牌号是{self.no},请问您要去哪里?')

    def stop(self):
        print('目的地到了,请您付款下车,欢迎再次乘坐')


class FamilyCar(Car):
    def __init__(self, type, no, name):
        super().__init__(type, no)
        self.name = name
    def stop(self):
        print('目的地到了,我们去玩儿吧')
    def start(self):
        print(f'我是{self.name},我的车牌号是{self.no},我的汽车我做主')


if __name__ == '__main__':
    taxi = Taxi('上海大众', '京A 9 3 5 3 2', '长城')
    taxi.start()
    taxi.stop()
    print('-'*30)
    familycar = FamilyCar('广汽丰田', '京B888888', '武大郎')
    familycar.start()
    familycar.stop()
复制代码

 

posted @   tuyin  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示