python 类的简单运用

 

定义一个手机类, 具有打电话和录音的功能,打电话的时候可以录音,也可以不录音。

class MobilePhone():
    def __init__(self,brand,type,color,phone_num):
        self.brand=brand #品牌
        self.type=type #类型
        self.color=color #颜色
        self.phone_num=phone_num #手机号

    def call(self):
        print('{}正在拨打电话...'.format(self.phone_num))
        try:
            choice=int(input('请问您是否需要录音?\n输入1(录音),输入2(不录音):'))
            if choice==1:
                self.record()
            elif choice==2:
                print('您未进入录音模式,请放心使用!')
            else:
                while True:
                    print('\033[31;1m您的输入有误!\033[0m')
                    choice = int(input('请输入以下选项执行操作:\n输入1(录音),输入2(不录音),输入3(结束通话):'))
                    if choice == 1:
                        self.record()
                        break
                    elif choice == 2:
                        print('您未进入录音模式,请放心使用!')
                        break
                    elif choice == 3:
                        print('感谢您的使用,再见!')
                        break
        except Exception as e:
            print("\033[31;1m您的输入有误,本次通话结束!\033[0m")

    def record(self):
        print('您的手机{},颜色为{},当前已进入通话录音...'.format(self.type,self.color))


# 初始化对象/实例化对象
phone=MobilePhone('iphone','iphone 11','奢侈灰','13458215862')
phone.call()

 

posted @ 2019-12-19 17:22  ITester软件测试小栈  阅读(359)  评论(0编辑  收藏  举报