如何修改现有 Java 类以重载 Groovy 中的运算符?

发布于 2024-09-24 01:57:02 字数 874 浏览 3 评论 0 原文

我正在实施一些数学课程,并且第一次发现 Java 的问题(繁琐且重复的代码),我确实无法通过切割器编码来克服这些问题。运算符重载确实非常方便,而且 Groovy 在其他一些事情上可以做得更好(我也可能在某个时候尝试 Scala)。

因此,我仍在用 Java 编写主要类(现在是 Complex 和 Matrix),但我将使用 Groovy 中的它们。

我更改了一些方法的名称 (.add) 以利用运算符重载 (.plus)。

现在,我的类与“Number”基类交互得很好,所以我可以使用:

complex.add(number); //or 
complex = complex + number;

并得到一个很棒的答案,但我不能使用:

complex = number + complex

因为 number 不理解 .add(Number) ,即使它理解,它也不会如果结果是一个复数,不知道如何处理该数字。

我可以写一个number.plus(complex)并将其添加到号码接口吗?代码实际上非常简单,我可以反转它:

Number plus(complex) {
     return complex.plus(this);
}

这适用于所有类型为 number 的东西,但我不知道 Groovy 是否可以在事后将其移植到 number 接口。 (此外,Number 和 Complex 都是用 Java 编写的,所以我认为这是 groovy 的一个插件)。

还有其他建议吗?我应该屈服并学习 Scala 吗?

哦,我意识到可能已经有一些软件包可以做到这一点,但其中一部分只是学习数学,我正在学习量子计算文本,并正在实施练习。

I'm implementing a few math classes and for the first time finding problems with Java (cumbersome and duplicated code) that I really can't overcome by cleaver coding. Operator overloading would be REALLY handy, and there are a few other things that Groovy can do a lot better (I may also try Scala at some point).

So I'm still writing the primary classes (Right now Complex and Matrix) in Java, but I'm going to use them from Groovy.

I changed the names of some methods (.add) to take advantage of operator overloading (.plus).

Now, my classes interact very well with the "Number" base class, so I can use:

complex.add(number); //or 
complex = complex + number;

and get an awesome answer, but I cannot use:

complex = number + complex

since number doesn't understand .add(Number) and even if it did, it wouldn't know how to handle the Number if it turned out to be a complex.

Can I write a number.plus(complex) and add it to the number interface? The code would actually be pretty easy, I could just reverse it:

Number plus(complex) {
     return complex.plus(this);
}

This would work for everything of type number, but I don't know if Groovy can graft this to the number interface after the fact. (Also, Number and Complex are in Java, so I kind of see this as an add-in for groovy).

Any other advice? Should I just give in and learn Scala?

Oh, I realize that there are probably packages out there that can do this already, but part of it is just learning the math, I'm working through a quantum computing text and am implementing the exercises as I go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

迷鸟归林 2024-10-01 01:57:02

您可以将所需的方法作为闭包添加到 Number 元类中。

例如(利用加法的交换性):

Number.metaClass.plus << { Complex complex ->
    complex.plus(delegate)
}

在此之后,任何 Number 子类也可以位于等式的左侧。确保使用追加运算符 << 以避免破坏其他重载的 plus 元方法。 Number 被引用为 delegate 而不是 this,因为它是一个闭包。

You can add the required methods as closures to the Number metaclass.

For example (taking advantage of commutativity of addition):

Number.metaClass.plus << { Complex complex ->
    complex.plus(delegate)
}

After this, any Number subclass can be on the left-hand side of the equation too. Make sure you use the append operator << to avoid clobbering other overloaded plus metamethods. Number is referenced as delegate instead of this since it's a closure.

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