欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何在Python中調(diào)用基類成員方法-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

成都創(chuàng)新互聯(lián)是專業(yè)的桐廬網(wǎng)站建設(shè)公司,桐廬接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行桐廬網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

小編給大家分享一下如何在Python中調(diào)用基類成員方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

在Python中,可以使用super()函數(shù)來調(diào)用基類的成員方法;語法為“super(type[, object-or-type])”。super()函數(shù)是用于調(diào)用父類(超類)的一個(gè)方法;可以用來解決多重繼承問題。

如何在Python中調(diào)用基類成員方法

可以使用super()函數(shù):

例:

class Derived(Base):
def meth(self):
super(Derived,self).meth()

Python super() 函數(shù)

 Python 內(nèi)置函數(shù)

描述

super() 函數(shù)是用于調(diào)用父類(超類)的一個(gè)方法。

super 是用來解決多重繼承問題的,直接用類名調(diào)用父類方法在使用單繼承的時(shí)候沒問題,但是如果使用多繼承,會涉及到查找順序(MRO)、重復(fù)調(diào)用(鉆石繼承)等種種問題。

MRO 就是類的方法解析順序表, 其實(shí)也就是繼承父類方法時(shí)的順序表。

語法

以下是 super() 方法的語法:

super(type[, object-or-type])

參數(shù)

type -- 類。object-or-type -- 類,一般是 self

Python3.x 和 Python2.x 的一個(gè)區(qū)別是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :

Python3.x 實(shí)例:

class A:     def add(self, x):         y = x+1

         print(y)class B(A):    def add(self, x):        super().add(x)b = B()b.add(2)  # 3

Python2.x 實(shí)例:

#!/usr/bin/python# -*- coding: UTF-8 -*-
 class A(object):   # Python2.x 記得繼承 object
    def add(self, x):         y = x+1
         print(y)class B(A):    def add(self, x):        super(B, self).add(x)b = B()b.add(2)  # 3

返回值

無。

實(shí)例

以下展示了使用 super 函數(shù)的實(shí)例:

實(shí)例

#!/usr/bin/python# -*- coding: UTF-8 -*-
 class FooParent(object):    def __init__(self):        self.parent = 'I\'m the parent.'
        print ('Parent')
    
    def bar(self,message):        print ("%s from Parent" % message)
 class FooChild(FooParent):    def __init__(self):        # super(FooChild,self) 首先找到 FooChild 的父類(就是類 FooParent),然后把類 FooChild 的對象轉(zhuǎn)換為類 FooParent 的對象
        super(FooChild,self).__init__()    
        print ('Child')
        
    def bar(self,message):        super(FooChild, self).bar(message)
        print ('Child bar fuction')
        print (self.parent)
 if __name__ == '__main__':    fooChild = FooChild()
    fooChild.bar('HelloWorld')

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

ParentChildHelloWorld from ParentChild bar fuction
I'm the parent.

看完了這篇文章,相信你對如何在Python中調(diào)用基類成員方法有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站題目:如何在Python中調(diào)用基類成員方法-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://chinadenli.net/article42/cdhihc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、網(wǎng)站內(nèi)鏈、外貿(mào)網(wǎng)站建設(shè)App開發(fā)、標(biāo)簽優(yōu)化、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站