私有方法和属性被共有方法调用

class Phone:
    __current_voltage=3 #当前手机运行的电压
    def __keep_single_core(self):#让手机单核运行的方法
        print("让CPU以单核模式运行")
    def call_by_5g(self):#定义一个共有的方法  调用私有的方法和属性
        if self.__current_voltage>=1:
            print("5g通话允许开启")
        else:
            self.__keep_single_core()
            print("电量低,开启单核模式")


#实例化某个对象
phone1=Phone()
#调用单核运行的方法 因为是私有的所以会报错
#phone1.__keep_single_core()
#print(phone1.__current_voltage)#因为属性是私有的所以也报错

phone1.call_by_5g()

 

class Phone:
    __is_5g_enable=False #当5g开启中
    def __check_5g(self): #判断私有成员_is_5g_enable的值
        #如果5g开启中
        if self.__is_5g_enable==True:
            print("5g 开启中")
        else:
            print("5g网络 关闭中")

    def call_by_5g(self):#定义一个共有的方法  调用私有的方法和属性
        self.__check_5g()
        print("正在通话中")
#实例化某个对象
phone1=Phone()
phone1.call_by_5g()

 

posted @ 2023-07-06 20:01  胖豆芽  阅读(9)  评论(0编辑  收藏  举报