Java支持运算符重载吗?
我正在开发一个项目,该项目有一个名为 Vector2 的对象,
public static class Vector2 {
public Vector2 (float x, float y) {
this.x = x;
this.y = y;
}
public float x;
public float y;
public static Vector2 ZERO = new Vector2 (0, 0);
public static Vector2 FORWARD = new Vector2 (0, 1);
public static Vector2 LEFT = new Vector2 (1, 0);
public static float Distance (Vector2 a, Vector2 b) {
return (float) Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
}
}
并且想要执行以下操作:
Vector2 a = new Vector2 (2.32, 453.12);
Vector2 b = new Vector2 (35.32, 532.32);
Vector2 c = a * b;
// c.x = 2.32*35.32
// c.y = 453.12*532.32
float num = 5;
c = a * num;
// c.x = 2.32*5
// c.y = 453.12*5
这可能吗?如果可以的话我该怎么做,它叫什么?提前致谢。
i am working on a project that has an object called Vector2
public static class Vector2 {
public Vector2 (float x, float y) {
this.x = x;
this.y = y;
}
public float x;
public float y;
public static Vector2 ZERO = new Vector2 (0, 0);
public static Vector2 FORWARD = new Vector2 (0, 1);
public static Vector2 LEFT = new Vector2 (1, 0);
public static float Distance (Vector2 a, Vector2 b) {
return (float) Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
}
}
and would like to do the following:
Vector2 a = new Vector2 (2.32, 453.12);
Vector2 b = new Vector2 (35.32, 532.32);
Vector2 c = a * b;
// c.x = 2.32*35.32
// c.y = 453.12*532.32
float num = 5;
c = a * num;
// c.x = 2.32*5
// c.y = 453.12*5
Is this possible? If so how can I do it, and what is it called? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,Java 不支持运算符重载。
仅供参考:如果您可以使用其他语言,那么 C++ 以及与 Java 非常相似的 C# 确实支持运算符重载。如果您的项目(例如光线追踪)有很多与向量相关的操作,那么我实际上会考虑使用 C# 这样的语言。
No, Java does not support operator overloading.
As an FYI: if it's possible for you to work with other languages, then C++ and, the very Java like, C# do support operator overloading. If your project, for example Ray Tracing, has a lot of vector related operations, then I'd actually consider a language like C#.
Java 不像 C++ 等语言那样允许运算符重载。请参阅本文。创建实用函数来适应您想要完成的任务。
Java does not allow operator overloading like languages such as C++. See this article. Make utility functions to accommodate what you want to accomplish.
Java 中没有运算符重载。你能做的最好的事情就是使用一种方法。
那么如果
a
和b
是Vector2
对象,你可以这样做There is no operator overloading in Java. The best you can do is use a method.
Then if
a
andb
areVector2
objects you can do this正如 Nadir 所说,它被称为“运算符重载”并且(无论好坏)它没有包含在 Java 规范中。如果您想查看它的实际效果,可以在 C++ 中找到它。
在 Java 中,您需要将以下方法添加到您的
Vector2
类中:As Nadir said, it's called "operator overloading" and (for better or worse) it was not included in the Java specification. You can find it in C++ if you want to see it in action.
In Java, you'd need to add the following methods to your
Vector2
class:您所做的称为运算符重载。虽然 Java 不支持它(Google 找到了大量关于该主题的帖子和意见),但一些在 JVM 上运行的高级语言支持它,例如 Groovy。因此,如果您能够将 Groovy 引入到您的项目中,您就可以实现这一目标。您的 Groovy 代码将编译为相同的 JVM 字节码,因此将与您的 Java 完全可互操作,并且可以调用项目中现有的 Java。
http://groovy.codehaus.org/Operator+Overloading
What you are doing is called operator overloading. And while Java does not support it (Google to find a myriad of posts and opinions on the topic), some higher level languages that run on the JVM do, like Groovy. So if you are able to introduce Groovy into your project, you can accomplish this. Your Groovy code will compile down to the same JVM bytecode, so will be totally interoperable with your Java and can call existing Java in your project.
http://groovy.codehaus.org/Operator+Overloading