国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python中類的一些方法分析

2020-02-23 05:53:00
字體:
來源:轉載
供稿:網友

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

先來看看下面這段代碼:

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

本文實例運行環境為Python2.7.6

運行結果如下:

in Provider.action 

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

總之一句話:

這里子類實現了父類delegate中所期望的action方法

再來看看下面這段代碼:

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

運行結果如下:

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

分別繼承父類的方法,替換父類的方法,擴展了父類的方法
Super類定義了delegate方法并期待子類實現action函數,Provider子類實現了action方法.

相信本文所述對大家Python程序設計的學習有一定的借鑒價值。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 双牌县| 安达市| 宣化县| 二连浩特市| 定州市| 建水县| 毕节市| 清水河县| 双柏县| 蒙自县| 井陉县| 安溪县| 安乡县| 岚皋县| 靖远县| 邵阳市| 鄄城县| 五大连池市| 阳春市| 增城市| 昌黎县| 定陶县| 班戈县| 文安县| 宁都县| 香格里拉县| 崇左市| 威宁| 武隆县| 青冈县| 永济市| 丰顺县| 织金县| 崇阳县| 洞头县| 内丘县| 炉霍县| 阿城市| 增城市| 辰溪县| 合江县|