99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

Python面向對象進階及類成員

2018-06-08 17:24 更新

再次了解多繼承

先來一段代碼

  1. #!/usr/bin/env python

  2. # _*_ coding:utf-8 _*_

  3. class A:

  4.    def bar(self):

  5.        print("BAR")

  6.        self.f1()

  7. class B(A):

  8.    def f1(self):

  9.        print("B")

  10. class C:

  11.    def f1(self):

  12.        print("C")

  13. class D(C, B):

  14.    pass

  15. obj = D()

  16. obj.bar()

執(zhí)行結果

  1. /usr/bin/python3.5 /home/ansheng/文檔/Python_code/sublime/Week06/Day03/s1.py

  2. BAR

  3. C

  4. Process finished with exit code 0

流程釋意:

  1. 創(chuàng)建了類A、B、C、D;

  2. D繼承了CB,B繼承了A,D內什么都不做,pass;

  3. 創(chuàng)建一個對象obj,類是D,當執(zhí)行Dbar方法的時候會先從C里面尋找有沒有bar方法;

  4. C內沒有bar方法,然后繼續(xù)從B里面查找,B里面也沒有,B的父類是AA里面有bar方法,所以就執(zhí)行了Abar方法;

  5. Abar方法首先輸出了BAR;

  6. 然后又執(zhí)行了self.f1()self=obj,相當于執(zhí)行了obj.f1();

  7. 執(zhí)行obj.f1()的時候先從C里面查找有沒有f1這個方法,C里面又f1這個方法;

  8. 最后就執(zhí)行C里面的f1方法了,輸出了C

執(zhí)行父類的構造方法

  1. lass Annimal:

  2.    def __init__(self):

  3.        print("Annimal的構造方法")

  4.        self.ty = "動物"

  5. class Cat(Annimal):

  6.    def __init__(self):

  7.        print("Cat的構造方法")

  8.        self.n = "貓"

  9.        # 尋找Cat類的父類,然后執(zhí)行父類的構造方法

  10.        super(Cat, self).__init__()

  11. mao = Cat()

  12. print(mao.__dict__)

執(zhí)行結果

  1. /usr/bin/python3.5 /home/ansheng/文檔/Python_code/sublime/Week06/Day03/s1.py

  2. Cat的構造方法

  3. Annimal的構造方法

  4. {'ty': '動物', 'n': '貓'}

  5. Process finished with exit code 0

先執(zhí)行了Cat的構造方法,然后又執(zhí)行了Annimal的構造方法。
第二種執(zhí)行父類的方法如下:

  1. Annimal.__init__(self)

不推薦使用

利用反射查看面向對象成員歸屬

  1. #!/usr/bin/env python

  2. # _*_ coding:utf-8 _*_

  3. class Foo:

  4.    def __init__(self, name):

  5.        self.name = name

  6.    def show(self):

  7.        print('show')

  8. obj = Foo("as")

  9. # 如果是類,就只能找到類里的成員

  10. print(hasattr(Foo, "show"))

  11. # 如果是對象,既可以找到對象,也可以找類里的成員

  12. print(hasattr(obj, "name"))

  13. print(hasattr(obj, "show"))

執(zhí)行結果

  1. /usr/bin/python3.5 /home/ansheng/文檔/Python_code/sublime/Week06/Day03/s2.py

  2. True

  3. True

  4. True

  5. Process finished with exit code 0

利用反射導入模塊、查找類、創(chuàng)建對象、查找對象中的字段

s1腳本文件內容:

  1. #!/usr/bin/env python

  2. # _*_coding:utf-8 _*_

  3. # 導入模塊

  4. m = __import__('s2', fromlist=True)

  5. # 去模塊中找類

  6. class_name = getattr(m, "Foo")

  7. # 根據類創(chuàng)建對象

  8. obj = class_name("ansheng")

  9. # 去對象中找name對應的值

  10. print(getattr(obj, 'name')

s2腳本文件內容

  1. #!/usr/bin/env python

  2. # _*_coding:utf-8 _*_

  3. class Foo:

  4.    def __init__(self, name):

  5.        # 普通字段,保存在對象中

  6.        self.name = name

執(zhí)行結果

  1. /usr/bin/python3.5 /home/ansheng/文檔/Python_code/sublime/Week06/Day04/s1.py

  2. ansheng

  3. Process finished with exit code 0

面向對象類成員之靜態(tài)字段

靜態(tài)字段存在類中,如下:

  1. #!/usr/bin/env python

  2. # _*_coding:utf-8 _*_

  3. # 靜態(tài)字段存在的意義就是將每個對象中重復的東西在類里面保存一份即可,這就是靜態(tài)字段

  4. class Provice:

  5.    # 靜態(tài)字段

  6.    contry = "China"

  7.    def __init__(self, name):

  8.        self.name = name

  9.    def show(self):

  10.        print(Provice.contry, self.name)

  11. hebei = Provice("河北")

  12. hebei.show()

  13. hubei = Provice("湖北")

  14. hubei.show()

執(zhí)行結果

  1. /usr/bin/python3.5 /home/ansheng/文檔/Python_code/sublime/Week06/Day04/s2.py

  2. China 河北

  3. China 湖北

  4. Process finished with exit code 0

類里面的成員類去訪問,對象內的成員用對象去訪問。

面向對象類成員之靜態(tài)方法

  1. #!/usr/bin/env python

  2. # _*_coding:utf-8 _*_

  3. class Foo:

  4.    # 靜態(tài)方法括號內沒有self,切方法前一行要加上@staticmethod

  5.    @staticmethod

  6.    def static():

  7.    # def static(arg1, arg2): # 也可以設置參數

  8.        print("static")

  9. # 靜態(tài)方法通過類名+方法名既可執(zhí)行

  10. Foo.static()

  11. # Foo.static("arg1", "arg2") 通過類調用的時候傳入對于的參數即可

  12. # 靜態(tài)方法也可以通過對象去訪問,對于靜態(tài)方法用類去訪問

  13. obj = Foo()

  14. obj.static()

執(zhí)行結果

  1. /usr/bin/python3.5 /home/ansheng/文檔/Python_code/sublime/Week06/Day04/s2.py

  2. static

  3. static

  4. Process finished with exit code 0

面向對象類成員之類方法

  1. #!/usr/bin/env python

  2. # _*_coding:utf-8 _*_

  3. class Foo:

  4.    # 創(chuàng)建類方法的時候需要在方法前面加上@classmethod

  5.    @classmethod

  6.    def ClassMethod(cls): # 并且方法的括號內必須帶有cls關鍵字,類方法的參數是當前類的類名

  7.        print("類方法")

  8. # 調用類方法

  9. Foo.ClassMethod()

執(zhí)行結果:

  1. /usr/bin/python3.5 /home/ansheng/文檔/Python_code/sublime/Week06/Day04/s2.py

  2. 類方法

  3. Process finished with exit code 0

面向對象類成員內容梳理

字段

1.靜態(tài)字段(每個對象都有一份)
2.普通字段(每個對象都不同的數據)

方法

1.靜態(tài)方法(無需使用對象封裝的內容)
2.類方法
3.普通方法(適用對象中的數據)

特性

1.普通特性(將方法未造成字段)

快速判斷,類執(zhí)行、對象執(zhí)行:

1.self —> 對象調用
2.無self —> 類調用


本文出自 “一盞燭光” 博客,謝絕轉載!

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號