属性方法:把一个方法变成一个静态属性
class Dog(object): n='wangcai' def __init__(self,name): self.name=name @property def eat(self): print('%s is eating %s'%(self.n,'骨头')) d=Dog('旺财') d.eat >>>wangcai is eating 骨头
class Dog(object): n='wangcai' def __init__(self,name): self.name=name self.__food=None @property #变成属性 def eat(self): print('%s is eating %s'%(self.name,self.__food)) @eat.setter #修改 def eat(self,food): print('set to food:',food) self.__food=food @eat.deleter #删除 def eat(self): del self.__food print('delete') d=Dog('旺财') d.eat d.eat='gutou' d.eat del d.eat