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)

posted @ 2023-11-24 17:48  vx_guanchaoguo0  阅读(16)  评论(0编辑  收藏  举报