類相當于一個容器,將狀態(tài)(數(shù)據(jù)屬性)和一些行為(方法)包裹在一起,今天我們就來講講Python中的方法.
要點:
1.類的實例方法 類的實例方法一般有一個顯著的特征就是會帶有self參數(shù),它的第一個正式參數(shù)名為self(這參數(shù)有點類似java里面的this),這些方法會訪問實例的屬性. 看下面的實例代碼: 揭秘一下這個調(diào)用的過程: 1).step1,step2把參數(shù)'arun','seema'傳給__init__()里面的data 2).step3 self參數(shù)就是ik1對象實例的引用 3).在step4調(diào)用printd()這個實例函數(shù)的時候,我們不需要把ik1傳遞self,python自動幫你搞定 2.類的靜態(tài)函數(shù) 類中的方法一般有self的方法也叫綁定對象方法,那Python中有沒有不帶self,當然有的,靜態(tài)函數(shù)方法就是其中一個
那么Python中為啥要有靜態(tài)函數(shù)呢: 場景就是和類相關(guān)的操作,但是又不會依賴和改變類、實例的狀態(tài),比如經(jīng)常有跟類有關(guān)系的函數(shù),我們希望它在運行時又不需要實例和類參與的情況下直接就能調(diào)用。調(diào)用靜態(tài)方法可以無需創(chuàng)建對象 舉個簡單的例子: 有一個機器人的類,有兩個方法重啟(do_Reset)和保存數(shù)據(jù)庫(save_DB),這兩個方法操作之前都需要檢查指令 robot1 = Robot('No1_Machine') robot1.do_Reset() robot1.save_DB() >> Reset done for :No1_Machine DB connection ready for: No1_Machine robot2 = Robot('No2_Machine') robot2.do_Reset() robot2.save_DB() >> Reset done for :No2_Machine DB connection ready for: No2_Machine 這樣是不是很麻煩,能不能把這個check_Indication()放到類里面,并且讓類的所有對象都能方便共享呢,有同學說簡單,挪進來,然后再把調(diào)用check_Indication的地方變成Robot.check_Indication() robot1 = Robot('No1_Machine') robot1.do_Reset() >> TypeError: unbound method check_Indication() must be called with Robot instance as first argument (got nothing instead) 直接報錯,根本不行,這個時候靜態(tài)函數(shù)就可以派上用場了 robot1 = Robot('No1_Machine') robot1.do_Reset() robot1.save_DB() robot2 = Robot('No2_Machine') robot2.do_Reset() robot2.save_DB() >> Reset done for :No1_Machine DB connection ready for: No1_Machine Reset done for :No2_Machine DB connection ready for: No2_Machine 3.類方法 類的方法就叫類方法,當我們要寫一個只在類中運行而不在實例中運行的方法. 比如我們想讓方法不在實例中運行,就可以用到類方法.也是用裝飾器@classmethod來修飾的 舉個例子我們有一個Student類,我們想記錄有多少個學生 def get_num_of_instance(cls_obj): return cls_obj.num_student class Student(object): num_student=0 def __init__(self): Student.num_student+=1 s1=Student() s2=Student() total_num=get_num_of_instance(Student) print total_num >> 2 其實我完全可以在類里面建一個函數(shù),然后獲得類的引用去獲取類的變量,這樣不是很簡單嗎: class Student(object): num_student=0 def __init__(self): Student.num_student+=1 @classmethod def get_num_of_instance(cls): return cls.num_student s1=Student() s2=Student() total_num=s1.get_num_of_instance() print total_num >> 2 total_num=s2.get_num_of_instance() print total_num >> 2 total_num=Student.get_num_of_instance() print total_num >> 2 這樣的好處是: 不管這個方式是從實例調(diào)用還是從類調(diào)用,它都用第一個參數(shù)把類傳遞過來.聰明的同學發(fā)現(xiàn)其實類方法的作用和靜態(tài)函數(shù)很像,確實很像,接著往下看 4.類方法,靜態(tài)函數(shù),實例方法之間調(diào)用關(guān)系 1). 靜態(tài)方法在訪問本類的成員時,只允許訪問靜態(tài)成員(即靜態(tài)成員變量和靜態(tài)方法),而不允許訪問實例成員變量和實例方法;實例方法則無此限制 2).實例方法可以訪問實例屬性和類屬性,也可以訪問靜態(tài)方法和類方法. 3). 類方法可以被對象調(diào)用,也可以被實例調(diào)用;傳入的都是類對象,主要用于工廠方法,具體的實現(xiàn)就交給子類處理 3). 靜態(tài)方法參數(shù)沒有實例參數(shù) self, 也就不能調(diào)用實例參數(shù),實例方法 |
|
來自: Four兄 > 《Python筆記》