1 實例方法
- 需要實例化對象才能使用的方法,使用過程中可能需要藉助對象的其他對象的方法完成
2 靜態方法
- 不需要實例化,通過類直接訪問
3 類方法
- 不需要實例化
class Person():
#類實例方法
def eat(self):
print(self)
print("eating")
#類方法
@classmethod
def play(cls):
print(cls)
print("playing")
#靜態方法
@staticmethod
def say():
print("saying")
p = Person()
p.eat
Person.play()
p.play
Person.say()
p.say()
結果:
<__main__.Person object at 0x000000000286FDD8>
eating
<class '__main__.Person'>
playing
<class '__main__.Person'>
playing
saying
saying
注:此篇不詳整,需再添加修改。