Python 物件導向相關

來源:互聯網
上載者:User

標籤:類型   inf   執行個體變數   使用   資訊   function   info   stat   def   

1. 類基本定義
  • 預設構造器為只有一個self參數,內容只有一行pass。
  • 方法: 
    • 執行個體方法:以執行個體本身self作為第一個參數。
    • 類方法:以類對象本身cls作為第一個參數,以@classmethod修飾。
    • 靜態方法:參數無要求,以@staticmethod修飾。
  • 變數: 
    • 執行個體變數:以self.開頭的變數。
    • 類變數:在類定義後、方法外定義的變數。
    • 變數的引用: 
      • 類變數: 
        • self.__class__.class_field_name:這種方法肯定成功。
        • self.class_field_name:如果成員變數中沒有與類變數同名的變數,則該方法也可以調用類變數。
        • class_name.class_field_name:通過類名直接引用。
      • 成員變數:self.instant_field_name
class Foo:    # 定義類變數    class_field = "class field"    field = "class normal field"    def __init__(self, x, y=1):        # 構造器定義,預設構造器為無參數,內容只有一行pass        # 可以設定參數,如上,則x必填,y選填        # 只能有一個構造器        print("Foo constructor", x, y)        # 定義成員變數        self.x = x        self.field = "instant normal field"    def instant_function(self, field="method field"):        print("執行個體方法")        print(self.class_field)        print(self.__class__.class_field)        # 對於同名變數的處理        print(field)        print(self.field)        print(self.__class__.field)        print(Foo.field)    @classmethod    def class_function(cls):        print("類方法")    @staticmethod    def static_function():        print("靜態方法")foo = Foo(1)foo.instant_function()# 輸出# Foo constructor 1 1# 執行個體方法# class field# class field# method field# instant normal field# class normal field# class normal field
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
2. 超類相關
  • 若子類不顯示定義構造器,則預設調用父類構造器。
  • 子類顯示定義了父類方法/構造器後,不管參數列表是否相同,都會覆蓋父類方法/父類構造器。 
    • 此處的方法包括執行個體方法、類方法、靜態方法。
  • __mro__
class Father:    @classmethod    def class_method(cls, message):        print(‘Father class method‘, message)    @staticmethod    def static_method():        print(‘Father static method‘)    def __init__(self):        print(‘father constructor‘)    def method1(self, message):        print(‘Father method1‘, message)    def method2(self, message):        print(‘Father method2‘, message)class Son(Father):    @classmethod    def class_method(cls):        print(‘Son class method‘)    @staticmethod    def static_method(message):        print(‘Son static method‘, message)    def __init__(self, x, y=2):        # 就算不調用父類也沒關係        # super方式調用父類構造器        super(Son, self).__init__()        print(‘son constructor‘)    def method1(self):        # 與父類方法名相同,參數不同        # 直接覆蓋父類方法        print(‘Son method1‘)    def method2(self, message):        # 與父類方法名相同,參數相同        # 覆蓋父類方法        # 調用父類方法        # super方式調用父類方法        super(Son, self).method2(message)        print(‘Son method2‘)son = Son(1)son.class_method()son.static_method("son")# 輸出# father constructor# son constructor# Son class method# Son static method son
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
3. 存取控制
  • 以雙底線__開頭的變數/方法,就是私人變數/方法。
  • 私人方法不能直接以__field_name__method_name進行訪問,但可以通過_class_name__field_name_class_name__method_name訪問。
  • Python中沒有絕對的私人。
  • 單底線:雖然不是私人的,但應被看作私人。如果有單底線函數_method(),則: 
    1. 在別的模組使用from somemodule import _method匯入方法成功。
    2. 在別的模組使用from somemodule import *則不會匯入_method方法。
class Foo:    __class_field_name = ‘private class field name‘    def __init__(self):        __instant_field_name = ‘private instant field name‘    def __private_method(self):        print(‘private method‘)foo = Foo()print(foo._Foo__class_field_name) #foo._Foo__private_method()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
4. 鴨子類型(Duck Typing)
  • 定義:動態類型的一種風格。在這種風格中,一個對象有效語義,不是由繼承自特定的類或實現特定的介面,而是由當前方法和屬性的集合決定。這個概念的名字來源於由James Whitcomb Riley提出的鴨子測試,“鴨子測試”可以這樣表述:“當看到一隻鳥走起來像鴨子、遊泳起來像鴨子、叫起來也像鴨子,那麼這隻鳥就可以被稱為鴨子。”
5. 對象資訊
  • type方法:擷取物件類型。
  • isinstance方法:判斷的物件類型(包括繼承)
  • dir方法:擷取對象所有屬性和方法。(結果為list
  • 有意向的朋友可以加扣扣群813622576一起交流學習,群內有免費資料給大家

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.