Java支持运算符重载吗?

发布于 2024-12-27 09:03:09 字数 877 浏览 1 评论 0原文

我正在开发一个项目,该项目有一个名为 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 技术交流群。

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

发布评论

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

评论(5

可爱咩 2025-01-03 09:03:09

不,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#.

少女七分熟 2025-01-03 09:03:09

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.

最好是你 2025-01-03 09:03:09

Java 中没有运算符重载。你能做的最好的事情就是使用一种方法。

public Vector2 multiply(Vector2 that){
    return new Vector2(this.x * that.x, this.y * that.y);
}

那么如果 abVector2 对象,你可以这样做

Vector2 c = a.multiply(b);

There is no operator overloading in Java. The best you can do is use a method.

public Vector2 multiply(Vector2 that){
    return new Vector2(this.x * that.x, this.y * that.y);
}

Then if a and b are Vector2 objects you can do this

Vector2 c = a.multiply(b);
音盲 2025-01-03 09:03:09

正如 Nadir 所说,它被称为“运算符重载”并且(无论好坏)它没有包含在 Java 规范中。如果您想查看它的实际效果,可以在 C++ 中找到它。

在 Java 中,您需要将以下方法添加到您的 Vector2 类中:

public Vector2 multiplyBy(Vector2 otherVector);

public Vector2 multiplyBy(float multiplier);

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:

public Vector2 multiplyBy(Vector2 otherVector);

public Vector2 multiplyBy(float multiplier);
游魂 2025-01-03 09:03:09

您所做的称为运算符重载。虽然 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

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