zoukankan      html  css  js  c++  java
  • 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('你是那偷心的贼')
  • 相关阅读:
    JVM字节码(七)
    JVM字节码(六)
    JVM字节码(五)
    JVM字节码(四)
    JVM字节码(三)
    JVM字节码(二)
    JVM字节码(一)
    JVM类加载器(五)
    JVM类加载器(四)
    php之 人员的权限管理
  • 原文地址:https://www.cnblogs.com/cong12586/p/11419730.html
Copyright © 2011-2022 走看看