python面向对象装饰器

@property 装饰过的函数返回的不再是一个函数,而是一个property对象
          装饰过后的方法不再是可调用的对象,可以看做数据属性直接访问。
@staticmethod 把没有参数的函数装饰过后变成可被实例调用的函数,
             函数定义时是没有参数的。
@classmethod 把装饰过的方法变成一个classmethod类对象,既能能被类调用又能被实例调用。
 注意参数是cls代表这个类本身。而是用实例的方法只能被实例调用。  

 

 

class Rectangle:
    def __init__(self,width,height):
        self.width = width
        self.height = height
    @property
    def area(self):
        return self.width*self.height
    @staticmethod
    def fun():
        return 'xxxxxx'
    @classmethod
    def show(cls):
        return 'yyyyyy'

 

posted @ 2021-02-06 13:54  Tudoo  阅读(108)  评论(0编辑  收藏  举报