溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python中的super怎么用

發(fā)布時間:2022-05-09 14:19:45 來源:億速云 閱讀:149 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“python中的super怎么用”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“python中的super怎么用”文章能幫助大家解決問題。

具體使用步驟:

1、首先打開python編輯器,新建一個python項目。

2、在python項目中直接使用super函數(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的對象轉換為類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')


輸出結果:

Parent

Child

HelloWorld from Parent

Child bar fuction

I'm the parent.


關于“python中的super怎么用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識,可以關注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI