python类方法

python类方法

学得有点不扎实,复习一下。类方法,实例方法,静态方法。
cls代表了类型,可以使用它继续构造新的对象或者使用静态变量。
静态函数就是跟类没半毛钱关系的,完全独立了。
记住,三种方法都可以通过实例或者类名去调用

class timemode:
    @staticmethod
    def showtime():
        print("nice")

    def showthis(self):
        print("sjho")

    @classmethod
    def s(cls):
        print(type(cls))
        return cls()

timemode.showtime()
a = timemode()
timemode.showthis(a)
timemode.s()
a.s().showthis()

# nice
# sjho
# <class 'type'>
# <class 'type'>
# sjho

猜你喜欢

转载自blog.csdn.net/weixin_42231070/article/details/82913876