class Person: def __init__(self): print("hello world") @staticmethod def get(): print("get") def send(self): print("send") # name = Person # # name() """ 无论有没有调用__init__都会自动执行,当类执行的时候,都会执行__init__()函数,因为__init__是函数的初始化函数,所以会 调用这个函数 @staticmethod 加上这个装饰器,能通过类名+方法直接调用函数,不需要实例化对象,用对象来调用 """ Person.get() # 能实现方法 Person.send() # 会报错 a = Person() # 实例化出一个对象 a.send() # 能实现调用,只有a能调用,其他的不能直接调用send方法,只有Person的子类才可以调用 a.get() # 能调用
一切都是对象,class 中@staticmethod 的作用
最新推荐文章于 2024-01-07 18:40:43 发布