python 实现电子产品类

  1. 修改属性和方法可以使用装饰器@property,@属性.setter
  2. 也可以直接 ele.XXX=...
  3. @property即可将方法变为属性,调用时候不需要加括号
  4. 错误在于使用super()的时候没有继承父类!

      

class electronical(object):
def __init__(self,Name,Id,Price):
self.Name=Name
self.Id=Id
self.Price=Price

class Computer(electronical):
"""
设计电脑类
"""
def __init__(self,Name,Id,Price,operatSystem,RAM,hardDisk,bareWeight,screenSize):
super().__init__(Name,Id,Price)
# self.comptName=comptName
# self.comptId=comptId
# self.comptPrice=comptPrice
self.operatSystem=operatSystem
self.RAM=RAM
self.hardDisk=hardDisk
self.bareWeight=bareWeight
self.screenSize=screenSize
@property
def getAll(self):
return self.Name,self.Price,self.operatSystem,self.RAM,self.hardDisk,self.bareWeight,self.screenSize
# def cutPrice(self,cut):
# return ('原价%d,打%f折后为:%2d'%(self.comptPrice,cut,self.comptPrice*cut))

class Phone(electronical):
"""手机类"""
def __init__(self,Name,Id,Price,phoneColor,ROM,isfiveG=False):
super().__init__(Name,Id,Price)
self.phoneColor=phoneColor
# self.phonePrice=phonePrice
self.ROM=ROM
self.isfiveG=isfiveG
@property
def getAll(self):
return self.Name,self.Price,self.ROM,self.phoneColor,self.isfiveG

class Pad(electronical):
def __init__(self,Name,Id,Price,padColor,padROM):
super().__init__(Name,Id,Price)
# self.padPrice=padPrice
self.padColor=padColor
self.padROM=padROM
@property
def getAll(self):
return self.Name,self.Price,self.padROM,self.padColor

猜你喜欢

转载自www.cnblogs.com/greenElio/p/12515646.html