1. @classmethod

class ClassMethod:
    def __init__(self, func):
        self.func = func

    def __get__(self, instance, owner):
        """
        类来调用,instance为None,owner为类本身。
        实例来调用,instance为实例,owner为类本身。
        :param instance:
        :param owner:
        :return:
        """

        def feedback(*args, **kwargs):
            return self.func(owner, *args, **kwargs)

        return feedback


class People:
    name = 'edward'

    @ClassMethod
    def say_hi(cls, msg):
        print(f'你好{cls.name},{msg}')


People.say_hi('你好帅')

 

2. @staticmethod

class StaticMethod:
    def __init__(self, func):
        self.func = func

    def __get__(self, instance, owner):
        def feedback(*args, **kwargs):
            return self.func(*args, **kwargs)

        return feedback


class People:
    name = 'edward'

    @StaticMethod
    def say_hi(x, y, z):  # say_hi = StaticMethod(say_hi)
        print('------>', x, y, z)


People.say_hi(1, 2, 3)

 

相关文章:

  • 2022-12-23
  • 2021-11-14
  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-16
  • 2022-02-19
猜你喜欢
  • 2021-10-20
  • 2021-07-04
  • 2021-08-16
  • 2021-09-23
  • 2022-12-23
  • 2021-12-17
  • 2021-09-08
相关资源
相似解决方案