Python面向對象進階及類成員
- 2020 年 1 月 6 日
- 筆記
再次了解多繼承
先來一段代碼
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
class A:
-
def bar(self):
-
print("BAR")
-
self.f1()
class B(A):
-
def f1(self):
-
print("B")
class C:
-
def f1(self):
-
print("C")
class D(C, B):
-
pass
obj = D()
obj.bar()
執行結果
/usr/bin/python3.5
/home/ansheng/文檔/Python_code/sublime/Week06/Day03/s1.py
BAR
C
Process finished with
exit code 0
流程釋意:
創建了類A、B、C、D;
D繼承了C和B,B繼承了A,D內什麼都不做,pass;
創建一個對象obj,類是D,當執行D的bar方法的時候會先從C裏面尋找有沒有bar方法;
C內沒有bar方法,然後繼續從B裏面查找,B裏面也沒有,B的父類是A,A裏面有bar方法,所以就執行了A的bar方法;
A的bar方法首先輸出了BAR;
然後又執行了self.f1(),self=obj,相當於執行了obj.f1();
執行obj.f1()的時候先從C裏面查找有沒有f1這個方法,C裏面又f1這個方法;
最後就執行C裏面的f1方法了,輸出了C
執行父類的構造方法
lass
Annimal:
-
def __init__(self):
-
print("Annimal的構造方法")
-
self.ty =
"動物"
class
Cat(Annimal):
-
def __init__(self):
-
print("Cat的構造方法")
-
self.n =
"貓"
-
# 尋找Cat類的父類,然後執行父類的構造方法
-
super(Cat,
self).__init__()
mao =
Cat()
print(mao.__dict__)
執行結果
/usr/bin/python3.5
/home/ansheng/文檔/Python_code/sublime/Week06/Day03/s1.py
Cat的構造方法
Annimal的構造方法
{'ty':
'動物',
'n':
'貓'}
Process finished with exit code 0
先執行了Cat的構造方法,然後又執行了Annimal的構造方法。
第二種執行父類的方法如下:
Annimal.__init__(self)
不推薦使用
利用反射查看面向對象成員歸屬
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
class
Foo:
-
def __init__(self, name):
-
self.name = name
-
def show(self):
-
print('show')
obj =
Foo("as")
# 如果是類,就只能找到類里的成員
print(hasattr(Foo,
"show"))
# 如果是對象,既可以找到對象,也可以找類里的成員
print(hasattr(obj,
"name"))
print(hasattr(obj,
"show"))
執行結果
/usr/bin/python3.5
/home/ansheng/文檔/Python_code/sublime/Week06/Day03/s2.py
True
True
True
Process finished with
exit code 0
利用反射導入模塊、查找類、創建對象、查找對象中的字段
s1腳本文件內容:
#!/usr/bin/env python
# _*_coding:utf-8 _*_
# 導入模塊
m = __import__('s2', fromlist=True)
# 去模塊中找類
class_name = getattr(m,
"Foo")
# 根據類創建對象
obj = class_name("ansheng")
# 去對象中找name對應的值
print(getattr(obj,
'name')
s2腳本文件內容
#!/usr/bin/env python
# _*_coding:utf-8 _*_
class
Foo:
-
def __init__(self, name):
-
# 普通字段,保存在對象中
-
self.name = name
執行結果
/usr/bin/python3.5
/home/ansheng/文檔/Python_code/sublime/Week06/Day04/s1.py
ansheng
Process finished with
exit code 0
面向對象類成員之靜態字段
靜態字段存在類中,如下:
#!/usr/bin/env python
# _*_coding:utf-8 _*_
# 靜態字段存在的意義就是將每個對象中重複的東西在類裏面保存一份即可,這就是靜態字段
class
Provice:
-
# 靜態字段
contry =
"China"
-
def __init__(self, name):
-
self.name = name
-
def show(self):
-
print(Provice.contry,
self.name)
hebei =
Provice("河北")
hebei.show()
hubei =
Provice("湖北")
hubei.show()
執行結果
/usr/bin/python3.5
/home/ansheng/文檔/Python_code/sublime/Week06/Day04/s2.py
China
河北
China
湖北
Process finished with
exit code 0
類裏面的成員類去訪問,對象內的成員用對象去訪問。
面向對象類成員之靜態方法
#!/usr/bin/env python
# _*_coding:utf-8 _*_
class
Foo:
-
# 靜態方法括號內沒有self,切方法前一行要加上@staticmethod
-
@staticmethod
-
def
static():
-
# def static(arg1, arg2): # 也可以設置參數
-
print("static")
# 靜態方法通過類名+方法名既可執行
Foo.static()
# Foo.static("arg1", "arg2") 通過類調用的時候傳入對於的參數即可
# 靜態方法也可以通過對象去訪問,對於靜態方法用類去訪問
obj =
Foo()
obj.static()
執行結果
/usr/bin/python3.5
/home/ansheng/文檔/Python_code/sublime/Week06/Day04/s2.py
static
static
Process finished with
exit code 0
面向對象類成員之類方法
#!/usr/bin/env python
# _*_coding:utf-8 _*_
class
Foo:
-
# 創建類方法的時候需要在方法前面加上@classmethod
-
@classmethod
-
def
ClassMethod(cls):
# 並且方法的括號內必須帶有cls關鍵字,類方法的參數是當前類的類名
-
print("類方法")
# 調用類方法
Foo.ClassMethod()
執行結果:
/usr/bin/python3.5
/home/ansheng/文檔/Python_code/sublime/Week06/Day04/s2.py
類方法
Process finished with
exit code 0
面向對象類成員內容梳理
字段
1.靜態字段(每個對象都有一份)
2.普通字段(每個對象都不同的數據)
方法
1.靜態方法(無需使用對象封裝的內容)
2.類方法
3.普通方法(適用對象中的數據)
特性
1.普通特性(將方法未造成字段)
快速判斷,類執行、對象執行:
1.self —> 對象調用
2.無self —> 類調用