VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > python入门教程 >
  • 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('你是那偷心的贼')
复制代码

 来源:https://www.cnblogs.com/cong12586/p/11419730.html



相关教程