返回信息流Composition comes with a great deal of flexibility. The member objects of
your new class are typically private, making them inaccessible to the client
programmers who are using the class. This allows you to change those
members without disturbing existing client code. You can also change the
member objects at run time, to dynamically change the behavior of your program. Inheritance, which is described next, does not have this
flexibility since the compiler must place compile-time restrictions on
classes created with inheritance.
主要合成和继承的区别。
为什么说 合成 “允许在运行时修改成员对象,并以此动态改变程序运行;而如果用继承创建一个类,编译器一定会加上某些编译时的限制”呢?
求大牛们科普。。有例子就更好了
这是一条镜像帖。来源:北邮人论坛 / java / #15292同步于 2010/7/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
thinking in java里面的一段话,看不懂
hmk
2010/7/23镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
组合的话,比如说A里边组合了一个B,那么可以用类似于A.setB(object)的方式来改变B字段的内容,将这个字段指向的实际B对象任意更换,换为B类的对象,或者B的任意一个子类的对象,因此其行为可以发生动态的改变(不同的子类对同一个方法的实现可以各不相同)。可是如果A继承B,那么B就成了A对象不可分割的一部分,无法随意更换。
然后java大湿们就会告诉你,没有标准答案的,所以要细心选择接口先。
然后不要 over design,
万一动了,请经常 refactoring
【 在 wks 的大作中提到: 】
: 一直搞不懂,接口本身要是需要改怎么办。
: --
: 代做C语言作业,男生勿扰,有意者站内联系。
: ...................