依赖于抽象不对了?

caoi 2006-09-20 12:19:38
UI Swing turorial online.
Related page:
http://java.sun.com/docs/books/tutorial/uiswing/layout/spring.html
source code:
http://java.sun.com/docs/books/tutorial/uiswing/layout/examples/SpringDemo2.java

My question is about this line:
SpringLayout layout = new SpringLayout();

As we know, class SpringLayout is implemented the interface LayoutManger, so this SpringLayout-to-LayoutManager is is-a relation. But when I replace it by:
LayoutManager layout = new SpringLayout();
Problem occurs, the IDE indicates that methods cannot be found.

Jeff Yan说过依赖于抽象的依赖反转原则DIP,这里怎么没有效果呢?我错哪里了?
...全文
218 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
caoi 2006-09-23
  • 打赏
  • 举报
回复
此问题在www.javaworld.com.tw上已经有了完整的回复。
http://www.javaworld.com.tw/jute/post/view?bid=29&id=167399&tpg=1&ppg=1&sty=1#167498
caoi 2006-09-20
  • 打赏
  • 举报
回复
to shoyonder(笑天)
迪米特法则(law of Detemetr)说的是一个对象对其他对象的了解度应该是最少。

而DIP说的依赖于抽象(比如接口)进行编程。比如ArrayList的建立:
List al = new ArrayList();而不是ArrayList al = new ArrayList();
shoyonder 2006-09-20
  • 打赏
  • 举报
回复
这个不是说依赖倒转原则,好像应该叫做迪米特法则巴。如果真的是父类子类关系,肯定没有问题。

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧