python 类直接调用属性和方法
class MyClass:
class_variable = "This is a class variable"
@staticmethod
def static_method():
print("This is a static method")
@classmethod
def class_method(cls):
print("This is a class method")
print("Class:", cls)
if __name__ == '__main__':
# 调用静态方法
MyClass.static_method()
MyClass.class_method()
print(MyClass.class_variable)
# 调用实例方法
my_instance = MyClass()
my_instance.static_method()
my_instance.class_method()
print(my_instance.class_variable)
本文来自博客园,作者:vx_guanchaoguo0,转载请注明原文链接:https://www.cnblogs.com/guanchaoguo/p/17854377.html