python 类方法、静态方法
class Foo(object):
def __init__(self, name):
self.name = name
def ord_func(self):
"""定义一个实例方法,至少有一个self参数"""
print("实例方法")
@classmethod
def class_fun(cls):
"""定义类方法,至少有一个cls参数"""
print("类方法")
@staticmethod
def static_fun():
"""定义静态方法, 无默认参数"""
print("静态方法")
f = Foo("中国")
# 调用实例方法
f.ord_func()
# 调用类方法
Foo.class_fun()
# 调用静态方法
Foo.static_fun()
来源:CSDN
作者:廷益--飞鸟
链接:https://blog.csdn.net/weixin_45875105/article/details/104607398