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()

  • 相关阅读:
    python 安装与pip安装
    使用通配符来解决数据1和11、12/13/14的问题
    数据库中一行变多行,拆分数据
    15-哈希表 HashTable
    13-自平衡二分搜索树 AVLTree
    12-并查集 UnionFind
    11-字典树 Trie
    10-线段树 Segment Tree
    09-堆 Heap(最大堆)
    08-映射 Map
  • 原文地址:https://www.cnblogs.com/ITester520/p/12068990.html
Copyright © 2011-2022 走看看