“这可不是泛型”-Bruce Eckel眼中的Java泛型[摘抄]
上一篇 / 下一篇 2008-01-10 17:56:18 / 个人分类:做着
| “这可不是泛型”-Bruce Eckel眼中的Java泛型ITPUB个人空间9f^#KD1N| |
| 2004-9-2 20:48:07 引至: 作者: 录入: |
Bruce Eckel在前不久写了一片批判Java的泛型的文章,结合他在OO上浸淫多年的功力,一眼就看出了Java的泛型和其他例如C++,Python,Ruby等等这些语言的泛型的差别。 不过平心而论,Bruce Eckel的批评是比较中肯的,因为他也看到了Java和其他这些面向对象的语言之间的差别,他也可以理解Java实现出了这样的泛型。 |
| “这可不是泛型”-Bruce Eckel眼中的Java泛型 (d7c&[:U6FK5yF9t0 |
| 2004-9-2 20:48:07 引至: 作者: 录入: |
Bruce Eckel在前不久写了一片批判Java的泛型的文章,结合他在OO上浸淫多年的功力,一眼就看出了Java的泛型和其他例如C++,Python,Ruby等等这些语言的泛型的差别。 不过平心而论,Bruce Eckel的批评是比较中肯的,因为他也看到了Java和其他这些面向对象的语言之间的差别,他也可以理解Java实现出了这样的泛型。 另外,如果大家不知道Ruby是什么,可以参考下面的网站:http://www.ruby-lang.org/en/ [补充]添加一段Bruce Eckel自己的评论: Guess what. I really don't care. You want to call it "generics," fine, implement something that looks like C++ or Ada, that actually produces a latent typing mechanism like they do. But don't implement something whose sole purpose is to solve the casting problem in containers, and then insist that on calling it "Generics." Of course, Java has long precedence in arrogantly mangling well- accepted meanings for things: one that particularly stuck in my craw was the use of "design pattern" to describe getters and setters. In JDK 1.4 we were told some lame way to use assertions which was a backward justification for them being disabled by default. JDK 1.4 also had to invent its own inferior logging system rather than using the openly created, well-tested and well-liked Log4J. And we've also been told many times about how Java has been as fast or faster than C++, or about how one or another feature is great and flawless. I point to the threads implementation which has had major changes quietly made from version to version with not so much as a peep of apology or admission that "hey, we really screwed up here." Or maybe I was just not on that particular announcement list.ITPUB个人空间j!GZxP"~.H def speak(anything): 注意到,这里对anything没有任何的类型限制,只是一个标识而已,假设这个类型能做一个叫做speak()的操作,就象实现了一个Interface一样,但是事实上你根本不需要去真的实现这样一个Interface,所以叫做隐式。现在我可以实现这样一个“狗狗和机器人”的例子: class Robot: a = Dog() Speak()方法不关心是否有参数传入,所以我可以传给它任何的东西,就象我传入的对象中支持的talk()方法一样。我相信在Ruby语言中的实现是和Python一致的。在C++中你可以做相同的事情:ITPUB个人空间ARm!xN6d class Robot {ITPUB个人空间og%K
e
J!U&e template void speak(T speaker) {ITPUB个人空间(VNT(L(S8pi int main() { |