Python筆記(九)

來源:互聯網
上載者:User

標籤:關於   prot   protected   通過   attr   不可   ict   python面向對   使用   

 1 #encoding=utf-8 2  3 # python進階編程 4  5     # python物件導向 6         # 建立類 7  8 # 無意中把Visual Studio Code的視窗調小了,查了一下,可以使用Ctrl+=放大視窗,使用Ctrl+-縮小視窗 9 10 11 12 class Person:                                   # 這裡類的申明用class13     ‘The baseclass of Human‘                    # 文檔字串,相當於類的描述14     ins_count=0                                 # 這裡申明的是類變數,在所有執行個體化對象中是公用的,類似於我們所說的靜態變數  15   16     def __init__(self,name,age,sex):            # 這裡方法的申明用def,__init__()表示初始化建構函式,注意這裡的init都是雙底線17         self.name=name                          # 類變數的申明,類似與我們的屬性,只在當前的執行個體化對象中訪問18         self.age=age                            19         self.sex=sex20         self.ins_count+=121 22     def logInfo(self):                          # 方法的定義,這種定義使用類似於Person.logInfo()這種方式調用是錯誤的     23         if(self.sex==1):                        # self代表類的執行個體,在定義類的方法時是必須的,但在調用時不必傳入相應的參數24             print ‘His name is‘,self.name,‘,age is‘,self.age25         else:26             print ‘Her name is‘,self.name,‘,age is‘,self.age    # 通過符號“.”來實現對屬性的訪問27         print self.ins_count28     29     def logAge(self):                           # 普通方法30         print ‘Age is‘,self.age31 32     @staticmethod33     def printClassName():                       # 靜態方法 無法訪問類和執行個體屬性34         print ‘Person‘35 36     @classmethod37     def printAttr(cls):                         # 類方法 可以訪問類屬性 無法訪問執行個體屬性38         print cls.ins_count39     40     def __del__(self):41         print ‘drop‘42 43 p=Person(‘hanmeimei‘,13,0)                      # 使用Person()的形式來建立類的執行個體,不需要像其他物件導向語言那樣使用new來執行個體化44 Person.printClassName()45 p.logInfo()46 47 48 print(getattr(p,‘name‘))49 50 ‘Python類的內建屬性:__dict__,類的屬性(包含一個字典由類的屬性資料群組成)‘51 print Person.__dict__ # 輸出類的資料屬性52 print Person.__name__ # 輸出類名53 print Person.__module__ # 輸出類所在的模組54 print Person.__doc__ # 輸出類的文檔字串55 print Person.__bases__ # 輸出類的所有父級的組成元組56 57 #del p # 當對象被建立時,就建立了一個引用,當這個對象不需要時,解譯器會在適當的時機將垃圾對象佔用的記憶體空間回收,可以定義__del__函數在對象銷毀時調用58 59 class Student(Person):                          # 左側即是類的繼承的寫法,使用 class child(parent)60     __serialNo=10086                            # 申明私人變數,在外部不可訪問61     def logAge(self):                           # 如果父類的方法不能滿足需求時,可以在子類中重寫父類的方法62         if(self.sex==1):                       63             print ‘His age is‘,self.age64         else:65             print ‘Her age is‘,self.age         66 67     def __addserialNo(self):                    # 定義私人方法,外部不能訪問68         self.__serialNo+=169 70 # 關於底線的說明,像__foo()__這種頭尾雙底線的表示特殊方法,一般表示系統定義的名字,如__init()__之類,_foo()單底線開頭這種則表示的是protected層級,__foo()雙底線開頭表示private層級71 72 73 s=Student(‘lilei‘,15,1)74 s.logInfo()75 s.logAge()76 77 # 熟悉物件導向的同志們可能發現這裡沒有重載,作者表示是的,我就是沒有,跟我的理念不相符,……

Python筆記(九)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.