python @classmethod 和 @staticmethod的区别
class testA():
def __init__(self):
print("初始化")
def a(self):
print("a")
return "a"
@staticmethod
def b():
print("b")
@classmethod
def c(cls):
x=cls.a(cls)
print("bbbbbbb")
cls.b()
print("x:",x)
print("c")
def d():
print("d")
x=testA()
x.a()
testA.b()
testA.c()
- @classmethod: 和 @staticmethod: 两种方式 都可以通过类直接调用,
当然也都可以 通过 实例进行调用
区别:@classmethod 可以调用本身类里的其他方法,而 @staticmethod不可以调用类里的其他方法
* 普通方法则需要 使用 实例 进行 调用 该方法
* 而d() 方法调用只能使用 类 进行调用: -> testA.d()
x = testA()
x.d() ->会失败