Topic: 给我一个使用继承的理由。 |
Print this page |
1.给我一个使用继承的理由。 | Copy to clipboard |
Posted by: wood Posted on: 2003-03-27 16:17 正在体会java。 突然发现,无法使用继承,明明是同一类型的方法,在每次使用时都要先区分类型,跟以前用c一样。 好羡慕书上的例子,用了继承好简洁,高手啊。 |
2.Re:给我一个使用继承的理由。 [Re: wood] | Copy to clipboard |
Posted by: jiangns3000 Posted on: 2003-03-27 17:55 继承是JAVA中(或所有OO语言)的精华之一。当我们在MFC中从一个类继承时,这意味着我们的程序重用了许多Microsoft MFC库设计者(精英程序员)的优秀代码。当在JAVA的程序中从JFC中类继承时意味着重用了许多JFC库设计者(精英程序员)的优秀代码。当你考虑或设计时,若A与B之间的关系是“Is-a”或“Is-like-a”关系时,用继承! 继承只是源代码级的一种重用。组件重用更多强调的是:封闭与接口实现的继承。 |
3.Re:给我一个使用继承的理由。 [Re: wood] | Copy to clipboard |
Posted by: jiangns3000 Posted on: 2003-03-27 18:17 继承给程序员的诱惑力很大,故继承常常有一种被滥用的趋向。 一个类的继承层次数大于7层,程序员理解该类时就会困难得多。 正是有了封装,才可能有继承,正是有了继承,才引出方法的多态。 |
4.Re:给我一个使用继承的理由。 [Re: wood] | Copy to clipboard |
Posted by: 南京肥牛 Posted on: 2003-03-28 20:23 现在JDK实现了JTable的Swing控件,你觉得不好看,功能不够多,那你是选择重新写一个还是集成一个JTable,再赋予一些新功能呢? |
5.Re:给我一个使用继承的理由。 [Re: wood] | Copy to clipboard |
Posted by: jiangns3000 Posted on: 2003-03-28 23:24 若你的“JTable”本质上与Swing的JTable相同(无质的差别),这自然是"Is - a" 或"Is- like - a"关系,故用继承。 若你的“JTable”本质上不是一个JTable型的组件(名你好像是JTable型但实质上不是,当然这是一个坏习惯)此时当然不用继承。由于它可能是如此之“新”,以致与Swing 的“JTable”都无关,故也不是"Has - a "关系,此时当然也不是集成。完全是一个“新”东西。恐怕它在JAVA中也只能从Object继承了,如果它还想继承点东西的话。 |
6.Re:给我一个使用继承的理由。 [Re: wood] | Copy to clipboard |
Posted by: floater Posted on: 2003-03-29 01:34 The general rule in OO is use composition over inheritance. This means, whenever possible, use composition rather than inheritance. Inheritance has performance hits and other issue, it's not a silver bullet, don't abuse it. After all it's just a way to deal with code complexity. MFC from MS has about 20 levels of inheritance, without special handling(message bypassing) it would be a turtle. Actually, this is a very good question. The answer is "Is-a" and "Is-like-a" relations between objects. These relations are non trivial by all means. For example, A rectangle is a polygon. Is this assertion true or not? A square is a rectangle. Is this true or not? Don't quote from books or references, because some of them are wrong, . Instead do your own thinking, if you are going to implement them, what can you do? |
7.Re:给我一个使用继承的理由。 [Re: wood] | Copy to clipboard |
Posted by: jiangns3000 Posted on: 2003-03-29 10:04 继承不是银弹,组合(或集成)更不是银弹。软件设计本身就是有创造性的。 到今天软件设计的银弹还没有找到。在上贴中给出的是 一种设计参考的原则,而不是一种死搬的教条。floater误以为只要是is-a 或is-like-a就是继承,has-a就是组合,软件设计不就变成一种机械的僵死的活了。原则性与设计时的灵活性及相关的经验相对合。 |
8.Re:给我一个使用继承的理由。 [Re: wood] | Copy to clipboard |
Posted by: jiangns3000 Posted on: 2003-03-29 10:29 软件工程的一大类理论(一门被认为是软件设计“哲学”的课程)中许多基本原则都是一种软件设计、软件管理、软件组织的参考性原则,有谁会 1)当作神圣不可侵犯的教条,死搬硬套,将它看作“圣经”? 2)当作空洞的无用的与实际无关的“原则”,将它丢进“垃圾堆”? |
9.Re:给我一个使用继承的理由。 [Re: jiangns3000] | Copy to clipboard |
Posted by: floater Posted on: 2003-03-29 13:11 jiangns3000 wrote: No, I didn't, that's why I said they are nontrivial. My point is just your last line |
10.Re:给我一个使用继承的理由。 [Re: jiangns3000] | Copy to clipboard |
Posted by: floater Posted on: 2003-03-29 13:14 jiangns3000 wrote: You think nobody does these? I saw tons of them. That's why I said what I said. And I don't like general sayings either, that's why I try to mention specifics wheneven possible(when I have time). |
11.Re:给我一个使用继承的理由。 [Re: jiangns3000] | Copy to clipboard |
Posted by: floater Posted on: 2003-03-29 13:16 jiangns3000 wrote: This is not my point. My point is: don't overuse inheritance without knowing the consequence. |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |