
关键字:@classmethod
class TEST:
@classmethod #类方法声明
def test(cls): #cls,class的缩写。必须存在,类似于普通方法中的self
print('classmethod test')
#类方法,保存在类中,通过类调用类方法
例 子:TEST.test()
本文介绍Python中的类方法概念及其使用方式。通过关键字@classmethod声明,类方法与类而非实例关联,可通过类名直接调用。文章包含示例代码,展示如何定义及调用类方法。

关键字:@classmethod
class TEST:
@classmethod #类方法声明
def test(cls): #cls,class的缩写。必须存在,类似于普通方法中的self
print('classmethod test')
#类方法,保存在类中,通过类调用类方法
例 子:TEST.test()

被折叠的 条评论
为什么被折叠?