本文实例分析了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
立即学习“Python免费学习笔记(深入)”;
在Super类中定义delegate()方法,delegate中调用self.action,在Provider子类中实现action方法。子类调用父类的delegate方法时,实际是调用自己的action方法。。
功能列表:底层程序与前台页面分离的效果,对页面的修改无需改动任何程序代码。完善的标签系统,支持自定义标签,公用标签,快捷标签,动态标签,静态标签等等,支持标签内的vbs语法,原则上运用这些标签可以制作出任何想要的页面效果。兼容原来的栏目系统,可以很方便的插入一个栏目或者一个栏目组到页面的任何位置。底层模版解析程序具有非常高的效率,稳定性和容错性,即使模版中有错误的标签也不会影响页面的显示。所有的标
0
总之一句话:
这里子类实现了父类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程序设计的学习有一定的借鉴价值。
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号