1、property
将方法伪装成属性
2、setter 修改属性
只有当被property装饰的方法,又实现了一个同名方法,且被setter装饰器
装饰了,在被装饰的方法赋值的时候,就触发被setter装饰器装饰的方法
3、deleter 删除属性
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Student: def __init__(self,name): self.__name = name @property def name(self): return self.__name @name.setter def name(self,new_name): self.__name = new_name @name.deleter def name(self): del self.__name zhangsan = Student('张三') print(zhangsan.name) zhangsan.name = '李四' print(zhangsan.name) del zhangsan.name print(zhangsan.__dict__)
4、classmethod 类方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Goods: __discount = 0.7 def __init__(name,price): self.__name = name self.__price = price @property def price(self): return self.__price * goods.__discount @classmethod def chang_price(cls,new): cls.__discount = new a = Goods('apple',10) print(a.price) Goods.chang_price(0.8) print(a.price)
5、staticmethod 普通方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Student: @staticmethod def login(): print('登陆成功')