- 论坛徽章:
- 0
|
Python是一种动态类型语言,拥有ducking type的特征,因此是天生支持接口,是这样理解吗?
- class Wind:
- def method(self):
- print "Wind::method"
- class Horse:
- def method(self):
- print "Horse::method"
- objects = [Wind(), Horse()]
- for o in objects:
- o.method()
复制代码
如果要在Java中完成这样的功能,就必须
1、让Wind和Horse继承同一个父类,使用父类操作method()。
2、提供一个接口,Wind和Horse分别实现method(),使用接口操作method()。
- interface Method {
- void method();
- }
- class Wind implements Method {
- void method() {
- print("Wind::method");
- }
- }
- class Horse implements Method {
- void method() {
- print("Horse::method");
- }
- }
复制代码
但是在Python就不需要了,既然这样为什么在有些框架里面又引入了interface,Python不是天生支持接口吗? |
|