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

查看:30
本文介绍了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)的语言,您可以查看 KotlinGroovy.或者,您可能会在 Java 编译器插件解决方案中找到运气.

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.

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

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