Python 没有多态
2016-04-26 19:43
615 查看
C++的多态表示: 基类指针指向派生类对象。 Python变量无类型,自然也就不存在多态。
python是一种动态语言,参数在传入之前是无法确定参数类型的
输出结果:
A
B
C
A
E
Traceback (most recent call last):
File "/Users/shikefu678/Documents/Aptana Studio 3 Workspace/demo/demo.py", line 33, in <module>
test(a),test(b),test(c),test(d),test(e),test(f)
File "/Users/shikefu678/Documents/Aptana Studio 3 Workspace/demo/demo.py", line 24, in test
arg.prt()
AttributeError: F instance has no attribute 'prt'
python是一种动态语言,参数在传入之前是无法确定参数类型的
class A: def prt(self): print "A" class B(A): def prt(self): print "B" class C(A): def prt(self): print "C" class D(A): pass class E: def prt(self): print "E" class F: pass def test(arg): arg.prt() a = A() b = B() c = C() d = D() e = E() f = F() test(a) test(b) test(c) test(d) test(e) test(f)
输出结果:
A
B
C
A
E
Traceback (most recent call last):
File "/Users/shikefu678/Documents/Aptana Studio 3 Workspace/demo/demo.py", line 33, in <module>
test(a),test(b),test(c),test(d),test(e),test(f)
File "/Users/shikefu678/Documents/Aptana Studio 3 Workspace/demo/demo.py", line 24, in test
arg.prt()
AttributeError: F instance has no attribute 'prt'
相关文章推荐
- 20160426 Python视频教程
- 为什么 Python 不支持函数重载?
- python遍历数组的两种方法
- Python 内置函数 range的使用
- Python数据分析学习笔记一
- Python 静态成员
- python 批量下载文件
- 在mac上搭建python环境
- python逆序切片
- Python核心编程 第六章练习
- 《python+opencv实践》一、基于颜色的物体追踪(上)
- python爬虫学习--pixiv爬虫(4)--代码优化
- python import 缺少解决方案
- python learning----顺序查找和二分法
- python 远程调度进程服务与客户端
- python 远程调度进程服务与客户端
- Python pencv documentation文档教程
- Python笔记(1)变量与表达式
- Python的局部变量和全局变量
- python安装mysqldb模块遇到的问题和解决方法