zoukankan      html  css  js  c++  java
  • 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('请问您是否需要录音?
    输入1(录音),输入2(不录音):'))
                if choice==1:
                    self.record()
                elif choice==2:
                    print('您未进入录音模式,请放心使用!')
                else:
                    while True:
                        print('33[31;1m您的输入有误!33[0m')
                        choice = int(input('请输入以下选项执行操作:
    输入1(录音),输入2(不录音),输入3(结束通话):'))
                        if choice == 1:
                            self.record()
                            break
                        elif choice == 2:
                            print('您未进入录音模式,请放心使用!')
                            break
                        elif choice == 3:
                            print('感谢您的使用,再见!')
                            break
            except Exception as e:
                print("33[31;1m您的输入有误,本次通话结束!33[0m")
    
        def record(self):
            print('您的手机{},颜色为{},当前已进入通话录音...'.format(self.type,self.color))
    
    
    # 初始化对象/实例化对象
    phone=MobilePhone('iphone','iphone 11','奢侈灰','13458215862')
    phone.call()

  • 相关阅读:
    2021NUAA暑假集训 Day3 题解
    2021NUAA暑假集训 Day2 题解
    2021NUAA暑期模拟赛部分题解
    CodeForces 1038D Slime
    UVA 11149 Power of Matrix
    UVA 10655 Contemplation! Algebra
    UVA 10689 Yet another Number Sequence
    HDU 4549 M斐波那契数列
    HDU 4990 Reading comprehension
    CodeForces 450B Jzzhu and Sequences
  • 原文地址:https://www.cnblogs.com/ITester520/p/12068990.html
Copyright © 2011-2022 走看看