总结:和类的关联性讲:属性方法>类方法>静态方法
属性方法@property:仅仅是调用方式不用+括号。
类方法@classmethod:访问不了累的属性变量,只可以访问类变量。
静态方法@staticmethod:仅仅是通过类名来调用这个函数而已,和类本身已经没有功能关系了,严格讲已经不是类的方法而是一个通过类名调用的函数而已(无法访问实例化的类的任何属性过着其他方法)。
在类中的方法加如下装饰器
属性方法:@property将方法变为类的一个静态属性,调用的时候无需加括号。对外隐藏实现细节,对于用户来讲,感觉就是在查一个写死的
class Flight(object): def __init__(self,name): self.name = name # flight_status def check_flight(self): print('checking flight %s: status'% self.name) status = 3 return status @property #为类的一个静态属性,调用的时候无需加括号。对外隐藏实现细节,对于用户来讲,感觉就是在查一个写死的静态属性 def flight_status(self): status = self.check_flight if status == 0: print('flight got canceled...') elif status == 1: print('flight is arrived...') elif status == 2: print('flight has departured already...') else: print('cannot confirm the flight status ....please check later') @flight_status.setter #对属性方法里面的参数进行修改 def flight_status(self,status): self.check_flight = status status_dict = { 0:'canceled', 1:'arrived', 2:'departured' } print('