python中類的一些方法分析
來(lái)源:易賢網(wǎng) 閱讀:864 次 日期:2014-09-26 08:31:03
溫馨提示:易賢網(wǎng)小編為您整理了“python中類的一些方法分析”,方便廣大網(wǎng)友查閱!

本文實(shí)例分析了python中類的一些方法,分享給大家供大家參考。具體分析如下:

先來(lái)看看下面這段代碼:

?12345678910 class Super: def delegate(self): self.action() class Provider(Super): def action(self): print 'in Provider.action' x = Provider() x.delegate()

本文實(shí)例運(yùn)行環(huán)境為Python2.7.6

運(yùn)行結(jié)果如下:

in Provider.action

在Super類中定義delegate()方法,delegate中調(diào)用self.action,在Provider子類中實(shí)現(xiàn)action方法。子類調(diào)用父類的delegate方法時(shí),實(shí)際是調(diào)用自己的action方法。。

總之一句話:

這里子類實(shí)現(xiàn)了父類delegate中所期望的action方法

再來(lái)看看下面這段代碼:

?12345678910111213141516171819202122232425262728293031323334353637 class Super: def delegate(self): self.action() def method(self): print 'super method' class Inherit(Super): pass class Replace(Super): def method(self): print "replace method" class Extended(Super): def method(self): print 'in extended class' Super.method(self) print 'out extended class' class Provider(Super): def action(self): print 'in Provider.action' x = Inherit() x.method() print '*'*50 y = Replace() y.method() print '*'*50 z = Extended() z.method() print '*'*50 x = Provider() x.delegate()

運(yùn)行結(jié)果如下:

?123456789 super method ************************************************** replace method ************************************************** in extended class super method out extended class ************************************************** in Provider.action

分別繼承父類的方法,替換父類的方法,擴(kuò)展了父類的方法

Super類定義了delegate方法并期待子類實(shí)現(xiàn)action函數(shù),Provider子類實(shí)現(xiàn)了action方法.

相信本文所述對(duì)大家Python程序設(shè)計(jì)的學(xué)習(xí)有一定的借鑒價(jià)值。

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看腳本欄目
易賢網(wǎng)手機(jī)網(wǎng)站地址:python中類的一些方法分析
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)