运算符在Java中重载 [英] Operator overloading in Java

查看:161
本文介绍了运算符在Java中重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我是否可以在Java中重载运算符?如果在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.

推荐答案

不,Java不支持用户自定义运算符重载。接近自定义运算符重载的Java的唯一方面是对字符串的处理,它导致常量的编译时连接或使用StringBuilder / StringBuffer的执行时连接。你不能定义自己的操作符,它们以相同的方式起作用。

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.

对于所做的类似Java(和基于JVM)的语言支持运营商重载,您可以查看 Groovy 。或者,您可能会对 Java编译器插件解决方案感到满意。

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

这篇关于运算符在Java中重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆