日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python面向对象进阶及类成员

發(fā)布時(shí)間:2025/5/22 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python面向对象进阶及类成员 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

再次了解多繼承

先來一段代碼

  • #!/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()

  • 執(zhí)行結(jié)果

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

  • BAR

  • C

  • Process finished with exit code 0

  • 流程釋意:

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

  • D繼承了C和B,B繼承了A,D內(nèi)什么都不做,pass;

  • 創(chuàng)建一個(gè)對(duì)象obj,類是D,當(dāng)執(zhí)行D的bar方法的時(shí)候會(huì)先從C里面尋找有沒有bar方法;

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

  • A的bar方法首先輸出了BAR;

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

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

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

  • 執(zhí)行父類的構(gòu)造方法

  • lass Annimal:

  • ? ?def __init__(self):

  • ? ? ? ?print("Annimal的構(gòu)造方法")

  • ? ? ? ?self.ty = "動(dòng)物"

  • class Cat(Annimal):

  • ? ?def __init__(self):

  • ? ? ? ?print("Cat的構(gòu)造方法")

  • ? ? ? ?self.n = "貓"

  • ? ? ? ?# 尋找Cat類的父類,然后執(zhí)行父類的構(gòu)造方法

  • ? ? ? ?super(Cat, self).__init__()

  • mao = Cat()

  • print(mao.__dict__)

  • 執(zhí)行結(jié)果

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

  • Cat的構(gòu)造方法

  • Annimal的構(gòu)造方法

  • {'ty': '動(dòng)物', 'n': '貓'}

  • Process finished with exit code 0

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

  • Annimal.__init__(self)

  • 不推薦使用

    利用反射查看面向?qū)ο蟪蓡T歸屬

  • #!/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"))

  • # 如果是對(duì)象,既可以找到對(duì)象,也可以找類里的成員

  • print(hasattr(obj, "name"))

  • print(hasattr(obj, "show"))

  • 執(zhí)行結(jié)果

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

  • True

  • True

  • True

  • Process finished with exit code 0

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

    s1腳本文件內(nèi)容:

  • #!/usr/bin/env python

  • # _*_coding:utf-8 _*_

  • # 導(dǎo)入模塊

  • m = __import__('s2', fromlist=True)

  • # 去模塊中找類

  • class_name = getattr(m, "Foo")

  • # 根據(jù)類創(chuàng)建對(duì)象

  • obj = class_name("ansheng")

  • # 去對(duì)象中找name對(duì)應(yīng)的值

  • print(getattr(obj, 'name')

  • s2腳本文件內(nèi)容

  • #!/usr/bin/env python

  • # _*_coding:utf-8 _*_

  • class Foo:

  • ? ?def __init__(self, name):

  • ? ? ? ?# 普通字段,保存在對(duì)象中

  • ? ? ? ?self.name = name

  • 執(zhí)行結(jié)果

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

  • ansheng

  • Process finished with exit code 0

  • 面向?qū)ο箢惓蓡T之靜態(tài)字段

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

  • #!/usr/bin/env python

  • # _*_coding:utf-8 _*_

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

  • class Provice:

  • ? ?# 靜態(tài)字段

  • ? ?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()

  • 執(zhí)行結(jié)果

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

  • China 河北

  • China 湖北

  • Process finished with exit code 0

  • 類里面的成員類去訪問,對(duì)象內(nèi)的成員用對(duì)象去訪問。

    面向?qū)ο箢惓蓡T之靜態(tài)方法

  • #!/usr/bin/env python

  • # _*_coding:utf-8 _*_

  • class Foo:

  • ? ?# 靜態(tài)方法括號(hào)內(nèi)沒有self,切方法前一行要加上@staticmethod

  • ? ?@staticmethod

  • ? ?def static():

  • ? ?# def static(arg1, arg2): # 也可以設(shè)置參數(shù)

  • ? ? ? ?print("static")

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

  • Foo.static()

  • # Foo.static("arg1", "arg2") 通過類調(diào)用的時(shí)候傳入對(duì)于的參數(shù)即可

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

  • obj = Foo()

  • obj.static()

  • 執(zhí)行結(jié)果

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

  • static

  • static

  • Process finished with exit code 0

  • 面向?qū)ο箢惓蓡T之類方法

  • #!/usr/bin/env python

  • # _*_coding:utf-8 _*_

  • class Foo:

  • ? ?# 創(chuàng)建類方法的時(shí)候需要在方法前面加上@classmethod

  • ? ?@classmethod

  • ? ?def ClassMethod(cls): # 并且方法的括號(hào)內(nèi)必須帶有cls關(guān)鍵字,類方法的參數(shù)是當(dāng)前類的類名

  • ? ? ? ?print("類方法")

  • # 調(diào)用類方法

  • Foo.ClassMethod()

  • 執(zhí)行結(jié)果:

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

  • 類方法

  • Process finished with exit code 0

  • 面向?qū)ο箢惓蓡T內(nèi)容梳理

    字段

    1.靜態(tài)字段(每個(gè)對(duì)象都有一份)
    2.普通字段(每個(gè)對(duì)象都不同的數(shù)據(jù))

    方法

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

    特性

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

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

    1.self —> 對(duì)象調(diào)用
    2.無self —> 類調(diào)用


    轉(zhuǎn)載于:https://blog.51cto.com/yw666/1917624

    總結(jié)

    以上是生活随笔為你收集整理的Python面向对象进阶及类成员的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。