美文网首页
python类装饰器

python类装饰器

作者: 周周周__ | 来源:发表于2019-07-10 11:24 被阅读0次
# -*- coding: utf-8 -*-


class Myproperty():
    def __init__(self, fun):
        print("执行Myproperty类的构造方法")   #调用Myproperty类时会首先运行它
        self.fun = fun

    def __get__(self, instance, owner):
        """
        :param instance: 代表school实例本身
        :param owner:  代表类School本身
        :return:
        """
        print('调用Myproperty的属性时将执行此方法')
        return self.fun(instance)


class School():
    """
    @name:学校名字
    @addr:学校地址
    @price:学费
    @num:招生人数
    """

    def __init__(self, name, addr, price, num):
        self.name = name
        self.addr = addr
        self.price = price
        self.num = num

    # @property
    @Myproperty  # 等价于-->>total=Myproperty(total)
    def total(self):
        "求总的学费"
        return self.price * self.num


school = School('浙江大学', '浙江省杭州市', 12000, 6000)
print(school.total)

参考https://blog.51cto.com/10836356/2112490

相关文章

网友评论

      本文标题:python类装饰器

      本文链接:https://www.haomeiwen.com/subject/bmbckctx.html