1. 程式人生 > >Python2.7 以及 Python 3.5的例項方法,類方法,靜態方法之間的區別及呼叫關係

Python2.7 以及 Python 3.5的例項方法,類方法,靜態方法之間的區別及呼叫關係

  今天很好奇Python2.7 和Python 3.5 的例項方法、類方法、靜態方法之間的

區別與聯絡。所以就做了兩個小實驗來測驗一下

Python3.5及以上

類的定義

class Test():
    def instanceMethod(self):  # 例項方法,必須傳入自身例項(self)作為引數
        print("I am the instancemethold,because I have the parmeter self")

    @classmethod
    def classMethod(cls): # 類方法,傳入類例項
print("I am the classMethod,because I have the @ modifier ,and my paramter has cls") @staticmethod def staticMethod(): # 靜態方法 print("I am the staticMethod,because I have the @ modifier ") def normalMethod(): # 普通方法 print("I am a noemal method Because I have nothing"
)

測驗結果:

一、例項對各種方法的呼叫:

t=Test()
t.instanceMethod()
t.classMethod()
t.staticMethod()
t.normalMethod()

# 輸出
I am the instancemethold,because I have the parmeter self
I am the classMethod,because I have the "@ modifier" ,and my paramter has cls
 I am the staticMethod,because I have the @ modifier 
Traceback (most recent call last): File "D:/Python/test.py", line 22, in <module> t.normalMethod() TypeError: normalMethod() takes 0 positional arguments but 1 was given

二、類對各種方法的呼叫:

t=Test()
Test.instanceMethod(t)
Test.classMethod()
Test.staticMethod()
Test.normalMethod()

# 輸出
I am the instancemethold,because I have the parmeter self
I am the classMethod,because I have the "@ modifier" ,and my paramter has cls
I am the staticMethod,because I have the @ modifier 
I am a normal method Because I have nothing

Python2.7中:

類的定義:

class Test():
    def instanceMethod(self):  # 例項方法,必須傳入自身例項(self)作為引數
        print("I am the instancemethold,because I have the parmeter self")

    @classmethod
    def classMethod(cls): # 類方法,傳入類例項
        print("I am the classMethod,because I have the @ modifier ,and my paramter has cls")

    @staticmethod
    def staticMethod(): # 靜態方法
        print(" am the staticMethod,because I have the @ modifier ")

    def normalMethod(): # 普通方法
        print("I am a noemal method Because I have nothing")

一、例項對各種方法的呼叫:

   ========================= RESTART: D:\Python\test.py =========================
>>> t=Test()
>>> t.instanceMethod   
<bound method Test.instanceMethod of <__main__.Test instance at 0x02BCC738>>
>>> t.instanceMethod()  # 例項可以呼叫例項方法,self為預設引數,不用傳
I am the instancemethold,because I have the parmeter self
>>> t.classMethod
<bound method classobj.classMethod of <class __main__.Test at 0x004FB378>>
>>> t.classMethod()  # 例項可以呼叫類方法 
I am the classMethod,because I have the @ modifier ,and my paramter has cls
>>> t.staticMethod
<function staticMethod at 0x02C09D30>
>>> t.staticMethod() # 例項可以呼叫靜態方法
 am the staticMethod,because I have the @ modifier 
>>> t.normalMethod  
<bound method Test.normalMethod of <__main__.Test instance at 0x02BCC738>>
>>> t.normalMethod() # 例項不可以呼叫普通方法

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    t.normalMethod()
TypeError: normalMethod() takes no arguments (1 given)
>>> 

二、類對各種方法的呼叫:

 ========================= RESTART: D:\Python\test.py =========================
>>> Test.instanceMethod() 

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    Test.instanceMethod()
TypeError: unbound method instanceMethod() must be called with Test instance as first argument (got nothing instead)
>>> Test.instanceMethod(t) # 類呼叫例項方法需傳入例項物件作為引數
I am the instancemethold,because I have the parmeter self
>>> Test.classMethod() # 類呼叫類方法
I am the classMethod,because I have the @ modifier ,and my paramter has cls
>>> Test.staticMethod  
<function staticMethod at 0x02C09D30>
>>> Test.staticMethod() # 類呼叫靜態方法
 am the staticMethod,because I have the @ modifier 
>>> Test.normalMethod 
<unbound method Test.normalMethod>
>>> Test.normalMethod()  #類不能呼叫普通方法

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    Test.normalMethod()
TypeError: unbound method normalMethod() must be called with Test instance as first argument (got nothing instead)
>>> 

總結 :

   以上實驗說明,例項可以訪問例項方法、類方法、靜態方法 ,普通方法不能訪問;類可以訪問例項方法(必須傳入類的例項作為引數)、類方法、靜態方法,普通方法不能訪問。
   例項可以訪問例項方法也可以訪問類方法,類可以訪問類方法也可以訪問例項方法,但是類方法在訪問例項方法的時候必須傳入類的例項作為引數,類可以理解為例項的一種。類本身可以訪問函式,但是例項確不可以。
  但是在Python2.7中類不能呼叫普通函式,在Python3.5h中類方法可以呼叫普通函式,具體體現在
Test.normalMethod()的使用當中。