classmethod自己定制

# # 利用描述符原理定义一个@classmethod
# class ClassMethod:
#     def __init__(self,func):
#         self.func = func
#     def __get__(self,instance, owner):
#         # 类来调用,instance为None,owner为类本身,
#         # 实例来调用,instance为实例,owner为类本身,
#         def feenback():
#             print(instance,owner)
#             print('在这里可以加功能')
#             return self.func(owner)
#         return feenback
# class People:
#     name = 'wangcong'
#     @ClassMethod
#     def say_hi(cls):
#         print(cls)
#         print("你好啊, %s" %cls.name)
#
# # say_hi = ClassMethod(say_hi)
# People.say_hi()
#
# p1 = People()
# print(p1.name)
# p1.say_hi()


# 类方法有参数
class ClassMethod:
    def __init__(self,func):
        self.func = func
    def __get__(self,instance,owner):
        def feedback(*args,**kwargs):
            print("在这里可以加工能")
            return self.func(owner,*args,**kwargs)
        return feedback
class People:
    name='linhaifeng'
    @ClassMethod # say_hi=ClassMethod(say_hi)
    def say_hi(cls,msg):
        print('你好啊,帅哥 %s %s' %(cls.name,msg))
People.say_hi('你是那偷心的贼')

p1=People()
p1.say_hi('你是那偷心的贼')

 

posted @ 2019-08-27 17:37  月为暮  阅读(115)  评论(0编辑  收藏  举报