Delegation VS. InheritanceInheritance:
the relationship between superclass and subclass cannot be changed during runtime.
modify superclass also changing subclass
IS-A relationDelegation:
One object forwards method call to another object, which is called delegate.
delegate can be changed at runtime, by changing the reference of delegate.
USE relation
java AWT1.0 is based on inheritance, now use Delegation Event Model. A very simple example:interface I{}
class A implements I{}
class B implements I{}class client {
void f(I i){ //delegation, i can be changed
i.method(); }
}
the relationship between superclass and subclass cannot be changed during runtime.
modify superclass also changing subclass
IS-A relationDelegation:
One object forwards method call to another object, which is called delegate.
delegate can be changed at runtime, by changing the reference of delegate.
USE relation
java AWT1.0 is based on inheritance, now use Delegation Event Model. A very simple example:interface I{}
class A implements I{}
class B implements I{}class client {
void f(I i){ //delegation, i can be changed
i.method(); }
}
解决方案 »
- 请教坐标旋转排序的问题
- 在WEB那边没人看...发这边吧.....JSP获得session中数据的问题
- 电脑同时插入50个优盘,如何把数据写入50个优盘里面?
- 线程奇怪的问题????
- 这种情况如何实现?
- 问一下jdk1.4 的按装方法?
- The server encountered an internal error () that prevented it from
- 请大家帮我一下,非常头疼的问题!关于JMF的异常!
- 有没有讲java与声音之间的编程知识的电子书?如有谢谢推荐。
- 哪位有项目的大虾可以给小弟一个练手的机会,或者给小弟一个以前做过的项目要求,让小弟实际练习练习
- 求邮箱(字符串)的验证算法。
- 大哥大姐们帮偶个忙。。。。新手送分了。谢谢简单的问题啊
class A implements I{void f(){....}}
class B implements I{void f(){....}}class Client implements I{
I delegate;
/* not important
public Client(){
delegate=new A();// default
}
*/
static setI(I i){
delegate=i ;
}
void f(){
...
delegate.f();
...
}
}public class Test{
public static void main(Strin[] args){
Client c=new Client;
c.set(new A());
c.f();
c.set(new B());
c.f();
}
}
将“流氓无赖”测试到底
——始于2003年7月
树欲止而风不停,行云流水匆匆去;
树梢蚂蚱凭空望,江边浪花碎巨石; 支持“流金岁月”!!!
发送框,少个“右键菜单,选择粘贴”;
——2003年12月24日am^@^
本次话题请参阅 Chapter 4: Item 14: Favor composition over inheritance.simonhappy()所说的,A于B之间确实是关联的关系,但是也是delegation的关系.
Effective Java前天刚买的啊,哈哈,还没看,赶快看看去楼