国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計的學習有一定的借鑒價值。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 寻乌县| 南宫市| 昌吉市| 罗源县| 灵台县| 商都县| 宁津县| 临沂市| 奉贤区| 闵行区| 双柏县| 休宁县| 额济纳旗| 康马县| 句容市| 雷波县| 桐柏县| 罗甸县| 时尚| 永宁县| 瓦房店市| 陈巴尔虎旗| 封开县| 上栗县| 海林市| 清镇市| 许昌县| 福海县| 高淳县| 常山县| 岳阳县| 彝良县| 万盛区| 乌什县| 行唐县| 宝兴县| 福州市| 榆林市| 任丘市| 天门市| 阿克苏市|