python 类中得静态法在类中实例方法中如何调用可以用self
在类中的实例方法中调用静态方法时,可以使用类名或者实例对象来调用。
使用类名调用静态方法:
class MyClass:
@staticmethod
def my_static_method():
print("This is a static method")
def my_instance_method(self):
MyClass.my_static_method()
obj = MyClass()
obj.my_instance_method()
使用实例对象调用静态方法:
class MyClass:
@staticmethod
def my_static_method():
print("This is a static method")
def my_instance_method(self):
self.my_static_method()
obj = MyClass()
obj.my_instance_method()
无论是使用类名还是实例对象调用静态方法,都可以在实例方法中正确调用静态方法。
原文地址: https://www.cveoy.top/t/topic/iHHk 著作权归作者所有。请勿转载和采集!