使用枚举值来表示二元运算符或函数

发布于 2024-08-24 15:07:44 字数 923 浏览 5 评论 0原文

我正在寻找一种优雅的方式来使用 Java 枚举中的值来表示操作或函数。我的猜测是,因为这是 Java,所以不会有一个好的方法来做到这一点,但无论如何,这里就是这样。我的枚举看起来像这样:

public enum Operator {
    LT,
    LTEQ,
    EQEQ,
    GT,
    GTEQ,
    NEQ;

    ...
}

其中 LT 表示 < (小于),LTEQ 表示 <= (小于或等于)等等 - 你明白了。现在我想实际使用这些枚举值来应用运算符。我知道我可以只使用一大堆 if 语句来做到这一点,但这是丑陋的 OO 方式,例如:

int a = ..., b = ...;
Operator foo = ...; // one of the enum values
if (foo == Operator.LT) {
    return a < b;
}
else if (foo == Operator.LTEQ) {
    return a <= b;
}
else if ... // etc

我想要做的是剪掉这个结构并使用某种一流的函数甚至是多态性,但我不太确定如何实现。类似:

int a = ..., b = ...;
Operator foo = ...;
return foo.apply(a, b);

或者甚至

int a = ..., b = ...;
Operator foo = ...;
return a foo.convertToOperator() b;

但据我所知,我认为不可能返回运算符或函数(至少,在不使用某些第三方库的情况下)。有什么建议吗?

I'm looking for an elegant way to use values in a Java enum to represent operations or functions. My guess is, since this is Java, there just isn't going to be a nice way to do it, but here goes anyway. My enum looks something like this:

public enum Operator {
    LT,
    LTEQ,
    EQEQ,
    GT,
    GTEQ,
    NEQ;

    ...
}

where LT means < (less than), LTEQ means <= (less than or equal to), etc - you get the idea. Now I want to actually use these enum values to apply an operator. I know I could do this just using a whole bunch of if-statements, but that's the ugly, OO way, e.g.:

int a = ..., b = ...;
Operator foo = ...; // one of the enum values
if (foo == Operator.LT) {
    return a < b;
}
else if (foo == Operator.LTEQ) {
    return a <= b;
}
else if ... // etc

What I'd like to be able to do is cut out this structure and use some sort of first-class function or even polymorphism, but I'm not really sure how. Something like:

int a = ..., b = ...;
Operator foo = ...;
return foo.apply(a, b);

or even

int a = ..., b = ...;
Operator foo = ...;
return a foo.convertToOperator() b;

But as far as I've seen, I don't think it's possible to return an operator or function (at least, not without using some 3rd-party library). Any suggestions?

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

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

发布评论

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

评论(2

山川志 2024-08-31 15:07:44

这不仅是可能的,而且在经常引用的 Effective Java,第二版中用作示例乔什·布洛赫。 我不想侵犯他的版权,会在网上寻找类似的示例...

好吧,我记得的代码可以在我之前链接的网站上免费获得。单击“下载本书中使用的代码示例”,然后查看effective2/examples/Chapter6/Item30/Operation.java

Not only is this possible, it's used as an example in the frequently referenced Effective Java, Second Edition by Josh Bloch. I don't want to step on his copyright, will look for a similar example online...

Okay, the code I remembered is freely available at the website I linked earlier. Click "Download the code samples used in this book", then look at effective2/examples/Chapter6/Item30/Operation.java.

以歌曲疗慰 2024-08-31 15:07:44
int a = ..., b = ...;
Operator foo = ...;
return foo.apply(a, b);

如果 foo 是对象并且 Operator 是基类并且具体运算符是实现 apply 的具体类,则可以执行此操作。

int a = ..., b = ...;
Operator foo = ...;
return foo.apply(a, b);

it is possible to do this if foo is object and Operator is base class and concrete operators are concrete classes that implement apply.

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