是否可以在 Java 中创建自定义运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10287952/
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
Is it possible to create a custom operator in Java?
提问by ahodder
Similar to Is it possible to create a new operator in c#?, is it possible to create your own operator for Java? I would initially say no since you can't overload, but then again, String supports + and += (implicitly through StringBuilder at execution time etc).
类似于是否可以在 c# 中创建新的运算符?,是否可以为 Java 创建自己的运算符?我最初会说不,因为你不能重载,但话说回来,String 支持 + 和 +=(在执行时隐式通过 StringBuilder 等)。
采纳答案by Bruno
Java doesn't allow for this.
Java 不允许这样做。
However, if you want to achieve this sort of syntax whilst being able to run your code on a JVM (and with other Java code), you could look at Groovy, which has operator overloading(and with which you could also use DSLs for short syntax which would have similar effects to using custom operators).
但是,如果您想在能够在 JVM 上运行您的代码(以及其他 Java 代码)的同时实现这种语法,您可以查看 Groovy,它具有运算符重载(并且您也可以使用它简称为 DSL与使用自定义运算符具有类似效果的语法)。
Note that defining custom operators (not just overloading) is a big deal in any language, since you would have to be able to alter the lexer and grammar somehow.
请注意,定义自定义运算符(不仅仅是重载)在任何语言中都是一件大事,因为您必须能够以某种方式改变词法分析器和语法。
回答by DNA
No, Java is not extensible in this way. You can't add operators, and you can't even further overload built-in operators like +
- even standard library classes like BigInteger have to use methods such as add()
rather than operators such as +
.
不,Java 不能以这种方式扩展。您不能添加运算符,甚至不能进一步重载内置运算符之类的+
- 即使像 BigInteger 这样的标准库类也必须使用add()
诸如+
.
Scala (another static JVM language) gets around this by using method calls rather than built-in operators, and allowing any characters in method names, so you can define new methods that appear to be operators, i.e.
Scala(另一种静态 JVM 语言)通过使用方法调用而不是内置运算符来解决这个问题,并允许方法名称中的任何字符,因此您可以定义看起来是运算符的新方法,即
a + 1
is syntactic sugar for:
是语法糖:
a.+(1)
回答by Kevin Welker
As all others have said, you absolutely cannot add new operators in Java. However, other JVM languages that are friendly to Java -- like Groovy -- will let you define new operators from existing operator tokens.
正如所有其他人所说,您绝对不能在 Java 中添加新的运算符。但是,其他对 Java 友好的 JVM 语言(例如 Groovy)将允许您根据现有的运算符标记定义新的运算符。
回答by ianpojman
No. Read this article for an argument of why they shouldn't be: http://java.dzone.com/articles/why-java-doesnt-need-operator
不。阅读这篇文章以了解为什么它们不应该是:http: //java.dzone.com/articles/why-java-doesnt-need-operator
You could use a different language, like Scala, to achieve this on the java platform. - https://stackoverflow.com/a/1991348/1342121
您可以使用不同的语言(如 Scala)在 java 平台上实现这一点。- https://stackoverflow.com/a/1991348/1342121
回答by logbasex
No. Java doesn't allow operator overloading. You can see more discussion here: https://javarevisited.blogspot.com/2011/08/why-java-does-not-support-operator.html
不可以。Java 不允许运算符重载。您可以在此处查看更多讨论:https: //javarevisited.blogspot.com/2011/08/why-java-does-not-support-operator.html
回答by Riley Lark
No, you can't overload special symbols for operators in Java.
不,您不能为 Java 中的运算符重载特殊符号。