標籤:類型 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(),則:
- 在別的模組使用
from somemodule import _method匯入方法成功。
- 在別的模組使用
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 物件導向相關