Java中的运算符重载

发布于 2024-08-10 10:53:23 字数 50 浏览 6 评论 0原文

请问Ja​​va中是否可以重载运算符?如果它在 Java 中的任何地方使用,请告诉我。

Please can you tell me if it is possible to overload operators in Java? If it is used anywhere in Java could you please tell me about it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(11

花之痕靓丽 2024-08-17 10:53:23

不,Java 不支持用户定义的运算符重载。 Java 唯一接近“自定义”运算符重载的方面是对字符串的 + 处理,这会导致常量的编译时连接或使用 StringBuilder/StringBuffer 的执行时连接。但您无法定义自己的运算符,它们的作用方式相同。

对于支持运算符重载的类似 Java(且基于 JVM)的语言,您可以查看 Kotlin Groovy。或者,您可能会遇到 Java 编译器插件解决方案

No, Java doesn't support user-defined operator overloading. The only aspect of Java which comes close to "custom" operator overloading is the handling of + for strings, which either results in compile-time concatenation of constants or execution-time concatenation using StringBuilder/StringBuffer. You can't define your own operators which act in the same way though.

For a Java-like (and JVM-based) language which does support operator overloading, you could look at Kotlin or Groovy. Alternatively, you might find luck with a Java compiler plugin solution.

谁人与我共长歌 2024-08-17 10:53:23

Java 中使用运算符重载来连接 String 类型:

String concat = "one" + "two";

但是,您不能定义自己的运算符重载。

Operator overloading is used in Java for the concatenation of the String type:

String concat = "one" + "two";

However, you cannot define your own operator overloads.

策马西风 2024-08-17 10:53:23

除了所有的人都指出 + 对字符串重载之外,- 对浮点和整数运算也重载,*/

[编辑]
% 对于浮点也是重载的,这对于具有 C 或 C++ 背景的人来说可能会有点惊讶。

In addition to all the people pointing out that + is overloaded for Strings, - is also overloaded for both floating point and integer operations, as are * and /.

[edit]
% is also overloaded for floating point, which can be a bit of a surprise for those with a C or C++ background.

送舟行 2024-08-17 10:53:23

Java 不允许运算符重载。首选方法是在类上定义一个方法来执行操作:a.add(b) 而不是 a + b。您可以在此处查看 Java 从类似 C 语言中遗漏的其他部分的摘要:功能从 C 和 C++ 中删除

Java does not allow operator overloading. The preferred approach is to define a method on your class to perform the action: a.add(b) instead of a + b. You can see a summary of the other bits Java left out from C like languages here: Features Removed from C and C++

时光礼记 2024-08-17 10:53:23

正如许多其他人已经回答的那样:Java 不支持用户定义的运算符重载。

也许这是题外话,但我想对我在一些答案中读到的一些内容发表评论。

关于可读性。
比较:

  1. c = a + b
  2. c = a.add(b)

再看看!
哪一个更具可读性?

允许创建用户定义类型的编程语言应该允许它们以与内置类型(或基本类型)相同的方式运行。

所以 Java 打破了通用编程的一个基本原则:
我们应该能够将内置类型的对象与用户定义类型的对象互换。
(您可能想知道:“他说的是‘内置对象’吗?”。是的,参见此处。)

关于字符串连接:

数学家使用符号 + 进行集合的交换运算。
所以我们可以确定 a + b = b + a。
字符串连接(在大多数编程语言中)不遵循这种常见的数学符号。

a := "你好";
b := "世界";
c := (a + b = b + a);

或者用Java:

String a = "你好";
字符串b =“世界”;
布尔值 c = (a + b).equals(b + a);

额外:
请注意,在 Java 中,相等性和同一性是如何混淆的。
==(相等)符号的含义是:
一个。原始类型的相等性。
b.对用户定义类型进行身份检查,因此,我们被迫使用 equals() 函数来实现相等性。
但是...这与运算符重载有什么关系?
如果语言允许运算符重载,则用户可以为相等运算符赋予正确的含义。

As many others have answered: Java doesn't support user-defined operator overloading.

Maybe this is off-topic, but I want to comment on some things I read in some answers.

About readability.
Compare:

  1. c = a + b
  2. c = a.add(b)

Look again!
Which one is more readable?

A programming language that allows the creation of user-defined types, should allow them to act in the same way as the built-in types (or primitive types).

So Java breaks a fundamental principle of Generic Programming:
We should be able to interchange objects of built-in types with objects of user-defined types.
(You may be wondering: "Did he say 'objects of built-in'?". Yes, see here.)

About String concatenation:

Mathematicians use the symbol + for commutative operations on sets.
So we can be sure that a + b = b + a.
String concatenation (in most programming languages) doesn't respect this common mathematical notation.

a := "hello";
b := "world";
c := (a + b = b + a);

or in Java:

String a = "hello";
String b = "world";
boolean c = (a + b).equals(b + a);

Extra:
Notice how in Java equality and identity are confused.
The == (equality) symbol means:
a. Equality for primitive types.
b. Identity-check for user-defined types, therefore, we are forced to use the function equals() for equality.
But... What has this to do with operator overloading?
If the language allows the operator overloading the user could give the proper meaning to the equality operator.

失眠症患者 2024-08-17 10:53:23

您不能自己执行此操作,因为 Java 不允许运算符重载。

不过,有一个例外。 ++= 是 String 对象的重载。

You can't do this yourself since Java doesn't permit operator overloading.

With one exception, however. + and += are overloaded for String objects.

仙女山的月亮 2024-08-17 10:53:23

可以尝试 Java 运算符重载。它有其自身的局限性,但如果您确实想使用运算符重载,则值得尝试。

One can try Java Operator Overloading. It has its own limitations, but it worth trying if you really want to use operator overloading.

去了角落 2024-08-17 10:53:23

只需将 Xtend 与 Java 代码一起使用即可。它支持运算符重载:

    package com.example;

@SuppressWarnings("all")
public class Test {
  protected int wrapped;

  public Test(final int value) {
    this.wrapped = value;
  }

  public int operator_plus(final Test e2) {
    return (this.wrapped + e2.wrapped);
  }
}

package com.example

class Test2 {

    new() {
        val t1 = new Test(3)
        val t2 = new Test(5)
        val t3 = t1 + t2
    }

}

在官方网站上,有每个运算符要实现的方法列表!

Just use Xtend along with your Java code. It supports Operator Overloading:

    package com.example;

@SuppressWarnings("all")
public class Test {
  protected int wrapped;

  public Test(final int value) {
    this.wrapped = value;
  }

  public int operator_plus(final Test e2) {
    return (this.wrapped + e2.wrapped);
  }
}

package com.example

class Test2 {

    new() {
        val t1 = new Test(3)
        val t2 = new Test(5)
        val t3 = t1 + t2
    }

}

On the official website, there is a list of the methods to implement for each operator !

烟凡古楼 2024-08-17 10:53:23

或者,您可以制作 Java Groovy 并重载这些函数来实现您想要的功能

//plus() => for the + operator
//multiply() => for the * operator
//leftShift() = for the << operator
// ... and so on ...

class Fish {
    def leftShift(Fish fish) {
        print "You just << (left shifted) some fish "
    }
}


def fish = new Fish()
def fish2 = new Fish()

fish << fish2

谁不想成为/使用 groovy? :D

不,您不能以同样的方式在 Java 中使用已编译的 groovy JAR。它仍然是 Java 的编译器错误。

Or, you can make Java Groovy and just overload these functions to achieve what you want

//plus() => for the + operator
//multiply() => for the * operator
//leftShift() = for the << operator
// ... and so on ...

class Fish {
    def leftShift(Fish fish) {
        print "You just << (left shifted) some fish "
    }
}


def fish = new Fish()
def fish2 = new Fish()

fish << fish2

Who doesnt want to be/use groovy? :D

No you cannot use the compiled groovy JARs in Java the same way. It still is a compiler error for Java.

心作怪 2024-08-17 10:53:23

与 C++ 不同,Java 不支持用户定义的运算符重载。重载是在java内部完成的。

我们可以以+(plus)为例:

int a = 2 + 4;
string = "hello" + "world";

这里,plus将两个整数相加并连接两个字符串。所以我们可以说Java支持内部运算符重载,但不支持用户定义的运算符重载。

Unlike C++, Java does not support user defined operator overloading. The overloading is done internally in java.

We can take +(plus) for example:

int a = 2 + 4;
string = "hello" + "world";

Here, plus adds two integer numbers and concatenates two strings. So we can say that Java supports internal operator overloading but not user defined.

巷子口的你 2024-08-17 10:53:23

Java 不支持开箱即用,但有一个编译器插件形式的解决方案,它允许您做到这一点。

请参阅 https://foojay.io/today/operator-overloading 上有关 id 的文章-in-java/

插件是 http://manifold.systems/

Java does not support this out of box but there is a solution in a form of a compiler plugin and it allows you to do that.

See this article about id on https://foojay.io/today/operator-overloading-in-java/

Plugin is http://manifold.systems/

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