Java中的运算符重载和覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3598707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:27:06  来源:igfitidea点击:

Operator overloading and overriding in Java

java

提问by rajesh

What is the difference between operator overloading and operator overriding?

运算符重载和运算符覆盖有什么区别?

Are they the same in inheritance and console program?

它们在继承和控制台程序中是否相同?

回答by amorfis

You cannot override (nor overload) operators in Java.

在 Java 中不能覆盖(也不能重载)运算符。

In some other languages you can, and difference between operator overloading and overriding is the same like between function overloading and overriting. E.g. in Scala operators are just functions.

在其他一些语言中,您可以,并且运算符重载和覆盖之间的区别与函数重载和覆盖之间的区别相同。例如,在 Scala 中,运算符只是函数。

回答by bragboy

There is no operator overloading/overriding in Java.

Java 中没有运算符重载/覆盖。

回答by YoK

Operator overloading and overriding are not supported in Java.

Java 不支持运算符重载和覆盖。

Check following desc quoted from : http://java.sun.com/docs/white/langenv/Simple.doc2.html

检查以下引自:http: //java.sun.com/docs/white/langenv/Simple.doc2.html

2.2.7 No More Operator Overloading

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.

2.2.7 不再有运算符重载

没有提供程序员可以重载标准算术运算符的方法。再一次,通过声明一个类、适当的实例变量和适当的方法来操作这些变量,可以很容易地实现运算符重载的效果。消除运算符重载可以极大地简化代码。

回答by haberdar

You can overload operator in C++ but not in Java. I wonder if you meant method overloading and method overriding? Method overloading is having two definitions for the same method signature. For example,

您可以在 C++ 中重载运算符,但不能在 Java 中重载。我想知道你的意思是方法重载和方法覆盖吗?方法重载对同一个方法签名有两个定义。例如,

int sum(int var1, int var2)
{
  return (var1+var2);
}

int sum(int var1, int var2, int var3)
{
  return (var1+var2+var3);
}

In object oriented programming, you override (redefine) a function that is inherited from ascendant (base) class. In a class hierarchy, when a function (method) in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

在面向对象编程中,您覆盖(重新定义)从上升(基)类继承的函数。在类层次结构中,当子类中的函数(方法)与其超类中的方法具有相同的名称和类型签名时,则称子类中的方法覆盖超类中的方法。

回答by Bjarke

There is a javac-plugin (an annotation processor like Lombok) called "Java-OO", which adds operator overloading to Java.

有一个名为“ Java-OO”的 javac-plugin(一种类似于 Lombok 的注释处理器),它为 Java 添加了运算符重载。

It allows you to add operator overloading to your own classes very easily. In addition to this, many of the built-in classes of the Java API also supports operator overloading when using this plugin. (For example: Instead of list.get(6) or map.get("hello") you can do list[6] and map["hello"])

它允许您非常轻松地将运算符重载添加到您自己的类中。除此之外,Java API 的许多内置类在使用此插件时也支持运算符重载。 (例如:代替 list.get(6) 或 map.get("hello") 你可以做 list[6] 和 map["hello"])

All you need to do is to include the .jar on the classpath when compiling with javac.

您需要做的就是在使用 javac 编译时将 .jar 包含在类路径中。

There are plugins for all major IDEs: Eclipse, Netbeans and IntelliJ IDEA.

有适用于所有主要 IDE 的插件:Eclipse、Netbeans 和 IntelliJ IDEA。