python 编写computer类

1、编写computer类,要求:
(1)含有私有属性:cpu、内存、硬盘;其中cpu用列表表示(主频和核数)、内存和硬盘用整数表示,内存的单位是M,硬盘的单位是G。
(2)公有属性:生产厂商、计算机名、IP地址,都用字符串表示
(3)构造函数,只可以对公有属性进行初始化,对于私有属性不能初始化
(4)编写设置和读取私有属性的方法(成员函数)
①cpu:输出函数要求输出格式为“X核YG”的方式,如:4核3.0G。
②内存:单位是M,当大于1024M时,则显示为G
③硬盘:单位是G,当大于1024G时,则显示为T。
注意:以上的三个设置方法要有正确性输入的判定。

class Computer:
    def __init__(self,maker,computer_name,ip):#初始化
        self.maker=maker
        self.computer_name=computer_name
        self.ip=ip
        
    def getcpu(self):           #cpu 判断 set get
        return self._cpu
    def setcpu(self,value):
        if type(value)==list:
            if type(value[0])==int and type(value[1])==float:
                self._cpu=value
            else:
                self._cpu=False
                if type(value[0])!=int:
                    print("主频不是整数!")
                else:
                    print("核数不是小数!")
        else:
            self._cpu=False
            print("cpu没有用列表表示!")
            
    def getmemory(self):        #内存 判断 set get
        return self._memory
    def setmemory(self,value):
        if type(value)==int:
            self._memory=value
        else:
            self._memory=False
            print("内存不是整数")
            
    def gethard(self):          #硬盘 判断 set get
        return self._hard
    def sethard(self,value):
        if type(value)==int:
            self._hard=value
        else:
            self._hard=False
            print("硬盘不是整数")
            
    cpu=property(getcpu,setcpu)			#私有属性 通过 c1.cpu=[4,1.0] 输入数据,不用初始化
    memory=property(getmemory,setmemory)
    hard=property(gethard,sethard)
    
    def put_computer(self):     					#输出
        print("\t生产厂商:",self.maker,"\n\t计算机名:",self.computer_name,"\n\tIP地址:",self.ip)
        if self._cpu==False:
            pass
        else:
            print("\tcpu: ",self._cpu[0],"核",self._cpu[1],"G")
            
        if self._memory==False:
            pass
        else:
            if self._memory/1024>1:					#内存满1024进位为G
                print("\t内存:",self._memory/1024,"G")
            else:
                print("\t内存:",self._memory/1024,"M")
                
        if self._hard==False:
            pass
        else:
            if self._hard/1024>1:					#硬盘满1024进位为T
                print("\t硬盘:",self._hard/1024,"T")
            else:
                print("\t硬盘:",self._hard/1024,"G")
if __name__=='__main__':							#检测代码
    c1=Computer('联想','Lenovo','192.168.1.1/24')
    c1.cpu=[4,1.0]
    c1.memory=2048
    c1.hard=2048
    c1.put_computer()

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值