Python中类属性有类属性和实例属性之分,类的方法也有不同的种类
实例方法
类方法
静态方法
例子:
class DemoMthd():
@staticmethod#静态方法
def static_mthd():
print("调用了静态方法")
@classmethod#类方法
def class_mthd(cls):
print("调用了类方法")
DemoMthd.static_mthd()
DemoMthd.class_mthd()
dm=DemoMthd()
dm.static_mthd()
dm.class_mthd()
对于这样的情况,对于类方法和静态方法,可以直接用类来调用也可以用实例来调用
但是不能使用实例属性,因为调用时类可能还没有实例化