Java 不能重载任何运算符。为什么?

发布于 2024-08-28 07:36:39 字数 218 浏览 9 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

_畞蕅 2024-09-04 07:36:39

http://java.sun.com/docs/white/langenv/Simple .doc2.html

没有提供任何方法
程序员可以超载标准
算术运算符。再一次,
运算符重载的影响可以是
通过声明一个同样容易实现
类,适当的实例变量,
以及适当的操纵方法
那些变量。消除运算符
超载会导致巨大的
简化代码。

最后的陈述当然是非常主观的。

http://java.sun.com/docs/white/langenv/Simple.doc2.html

There are no means provided by which
programmers can overload the standard
arithmetic operators. Once again, the
effects of operator overloading can be
just as easily achieved by declaring a
class, appropriate instance variables,
and appropriate methods to manipulate
those variables. Eliminating operator
overloading leads to great
simplification of code.

The last statement is of course very subjective.

尤怨 2024-09-04 07:36:39

实际上,它确实支持运算符重载……但其性质非常有限,仅内置。例如,除了通常的算术之外,“+”对于字符串来说也是重载的。

当然,大多数人都想知道为什么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.

朱染 2024-09-04 07:36:39

设计目标是让java比c++更简单

我们想要建立一个系统
无需太多即可轻松编程
深奥的训练并利用
今天的标准做法。所以即使
尽管我们发现 C++ 是
不合适,我们将Java设计为
尽可能接近 C++,以便
使系统更容易理解。
Java省略了很多很少使用的、不好的
C++ 的易理解、易混淆的特性
根据我们的经验,这会带来更多
悲大于利。

从这里:
Java 语言:概述

it's design goals to make java simpler than c++

We wanted to build a system that could
be programmed easily without a lot of
esoteric training and which leveraged
today’s standard practice. So even
though we found that C++ was
unsuitable, we designed Java as
closely to C++ as possible in order to
make the system more comprehensible.
Java omits many rarely used, poorly
understood, confusing features of C++
that, in our experience, bring more
grief than benefit.

from here:
The Java Language: An Overview

念三年u 2024-09-04 07:36:39

当您在 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.

我不是你的备胎 2024-09-04 07:36:39

请注意,存在一个异常情况,即 java.lang.String 重载了“plus”运算符。

Note that there's an anomaly in that the 'plus' operator is overloaded for java.lang.String.

一笑百媚生 2024-09-04 07:36:39

早在那时,就有消息称 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文