首页 > 编程语言 > 详细

【Python面向对象】(9) 装饰器之类装饰器

时间:2020-04-15 20:45:46      阅读:58      评论:0      收藏:0      [点我收藏+]

类的上方装饰方法,那么该方法会成为该类的方法,可以通过实例调用

"""
类的装饰器
"""


def f(self):
    print("{}要吃东西".format(self.name))
    print("0000000")


def eat(cls):
    """ 吃东西装饰器 """
    # cls.eat = lambda self: print("{}要吃东西".format(self.name))
    cls.eat = f
    return cls


@eat
class Cat(object):
    """ 猫类 """
    def __init__(self, name):
        self.name = name


if __name__ == "__main__":
    cat = Cat("小黑")
    cat.eat()
    # 输出:
    # 小黑要吃东西
    # 0000000

 

【Python面向对象】(9) 装饰器之类装饰器

原文:https://www.cnblogs.com/ac-chang/p/12707399.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!