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

首頁 > 編程 > Python > 正文

python中類的一些方法分析

2019-11-25 18:11:51
字體:
供稿:網(wǎng)友

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

先來看看下面這段代碼:

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

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

運行結(jié)果如下:

in Provider.action 

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

總之一句話:

這里子類實現(xiàn)了父類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() 

運行結(jié)果如下:

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

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

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 肥西县| 峨眉山市| 塘沽区| 富民县| 重庆市| 扎兰屯市| 天祝| 宁化县| 苍溪县| 信阳市| 手游| 绥棱县| 金堂县| 新田县| 通化市| 荥阳市| 永宁县| 灌南县| 房产| 阳东县| 闸北区| 澎湖县| 城口县| 米脂县| 棋牌| 长乐市| 卢龙县| 葵青区| 平利县| 南雄市| 南漳县| 泰州市| 高邮市| 岑巩县| 太康县| 株洲市| 广宁县| 洞口县| 象山县| 南汇区| 佛学|