class MyClass:
@classmethod
def my_class_method(cls, arg1, arg2):
# 使用cls参数访问类的属性和调用类的方法
print("This is a class method")
print("Arguments:", arg1, arg2)
# 调用类方法,不需要创建类的实例
MyClass.my_class_method("Hello", "World")
'''
This is a class method
Arguments: Hello World
'''
class Animal:
_feature= "delicious" # 定义了类中属性 特征
def __init__(self,name,color):
self.name=name
self.color=color
@classmethod
def get_feature(cls):
print("所有的动物都很%s,尤其是蝙蝠"%cls._feature)
# 不需要实例化对象 可以直接调用方法
Animal.get_feature()#所有的动物都很delicious,尤其是蝙蝠