Java 不能重载任何运算符。为什么?
可能的重复:
Java 运算符重载
在c++中,我们可以执行运算符重载。但Java也是一种面向对象的语言。那么为什么java不支持重载呢?
Possible Duplicate:
Java operator overload
In c++, we can perform the operator overloading. But Java is also a Object oriented language. So why java doesn't support overloading?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
http://java.sun.com/docs/white/langenv/Simple .doc2.html
最后的陈述当然是非常主观的。
http://java.sun.com/docs/white/langenv/Simple.doc2.html
The last statement is of course very subjective.
实际上,它确实支持运算符重载……但其性质非常有限,仅内置。例如,除了通常的算术之外,“+”对于字符串来说也是重载的。
当然,大多数人都想知道为什么Java不支持用户定义的运算符重载。 :-) 最简单的答案似乎是,Java 的创建者当时没有找到任何干净的方法将其添加到语言中,同时不会在此过程中使 Java 变得一团糟(如 C++)。
Actually, it does support operator overloading... of a very limited, built-in only nature. For instance "+" is overloaded for String's in addition to the usual arithmetic.
Of course, most people want to know why Java does not support user-defined operator overloading. :-) The simplest answer seems to be that the Java creators did not, at the time, see any clean way to add it to the language without making Java a mess (like C++) in the process.
设计目标是让java比c++更简单
从这里:
Java 语言:概述
it's design goals to make java simpler than c++
from here:
The Java Language: An Overview
当您在 C++ 中使用运算符重载时,代码会更难理解。也许这就是 Java 开发人员决定不实现它的原因。
实际上,富含重载运算符的代码可能会非常具有误导性并且难以阅读,就像带有大量宏的代码一样。
The code is harder to understand when you use operator overloading in c++. May be that was the reason why Java developers decided not to implement it.
Really the code rich with overloaded operators can be very misleading and hard to read, just like the code with a plenty of macros.
请注意,存在一个异常情况,即
java.lang.String
重载了“plus”运算符。Note that there's an anomaly in that the 'plus' operator is overloaded for
java.lang.String
.早在那时,就有消息称 Java 团队研究了其他语言(主要是 C++),并试图就 Java 中包含(或排除)哪些语言级别的功能做出判断。
运算符重载是 C++ 中的一个重要新功能,许多崭露头角的程序员使用它以有趣的方式解决问题。不幸的是,大多数这些方法都有缺陷,并且很少有程序员以“程序员可移植”的方式重载运算符。因此,阅读程序时常常会错过“+”运算符(或其他操作符)中引入的“神奇”代码。除了数字(向量等)之外,您对于“+”的含义也存在分歧。
因此,封面故事(不知道是否属实)是 Java 团队发现您可以只命名方法 sum.plus(...) ,并且很明显该调用是对用户编写的代码(可能会重载) )而保留的操作“+”等只会执行语言定义的操作。这会增加某些类型代码的可读性,但会增加一些额外的单词。
这是真的吗,还是试图用合理化来回填历史?我不知道。也许语言实现者只是忙于让 Java 正常工作,而没有在发布日期之前解决运算符重载问题。不管怎样,它在 95% 的情况下都极大地提高了 Java 的可读性,但代价是让向量数学变得有点冗长。
Way back when, the news was that the Java team looked at other languages (primarily C++) and tried to make a judgment call as to what language level features to include (or exclude) from Java.
Operator overloading was a big new feature in C++ and lots of budding programmers used it to solve problems in interesting ways. Unfortunately, most of those ways were buggy, and few programmers overloaded operators in ways that were "programmer portable". As a result, reading a program would often miss that "magic" code had been introduced in the "+" operator (or something else). Outside of numbers (vectors, etc.) you also had disagreements as to what "+" might mean.
So the cover story (no idea if it was true) was that the Java team saw that you could just name methods sum.plus(...) and it would be obvious that the call was to user written code (which could be overloaded) while the reserved operations "+", etc. would only did what the language defined. This would increase some types of code readability at the expense of a few extra words.
Is it true, or is it an attempt to back fill history with rationalizations? I don't know. Perhaps the language implementers just were busy trying to get Java working and didn't get around to operator overloading by release date. Either way, it greatly increases the readability of Java in 95% of all cases, at the expense of making vector math a bit wordy.