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

发布时间 2023-07-06 20:05:30作者: 胖豆芽
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()