Java枚举问题

发布于 2024-08-22 16:06:45 字数 306 浏览 1 评论 0原文

我有两个 Enum,如下所示:

enum Connector {
    AND, OR, XOR;
}

enum Component {
    ACTIVITY
}

现在,我在类 Event 中有一个名为 follower 的变量。此变量(follower)可以(并且应该具有)来自上述两个 Enum 中的任何一个的值。

那么,我应该为 follower 变量提供什么数据类型?

I have two Enums as follows:

enum Connector {
    AND, OR, XOR;
}

enum Component {
    ACTIVITY
}

Now, I have a variable named follower in a class Event. This variable (follower) can have (and should have) value from either of the above two Enums.

So, What datatype should I give to the follower variable?

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

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

发布评论

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

评论(5

妄司 2024-08-29 16:06:45

声明 follower 字段的接口。

public interface Follower {
    // any methods
}

并让两个枚举都实现该接口。

public enum Connector implements Follower {
    AND, OR, XOR;
}


enum Component implements Follower {
    ACTIVITY
}

然后你可以声明你的字段:

Follower follower = Connector.OR;  

或者

Follower follower = Component.ACTIVITY;

这比将字段声明为 Enum< 有几个明显的优势? extends Follower> (我能想到的)。通过这种方式,您可以自由地向 Follower 接口添加方法,而无需将来修改字段,而您无法控制 Enum 类型,因此如果您决定如果Follower需要一个方法,那么您必须在每个地方更改声明。您的场景可能永远不会出现这种情况,但使用这种方式的成本很低,这是很好的防御实践。

第二个稍微不太重要的优点,更多的是关于品味:它避免了类型中的泛型,当您包含通配符时,泛型可能会变得不太可读。

Declare an interface for the follower field.

public interface Follower {
    // any methods
}

And have both the enums implement that interface.

public enum Connector implements Follower {
    AND, OR, XOR;
}


enum Component implements Follower {
    ACTIVITY
}

Then you can declare your field:

Follower follower = Connector.OR;  

Or

Follower follower = Component.ACTIVITY;

This has a couple distinct advantage over declaring the field as an Enum<? extends Follower> (that I can think of). With this way you are free to add methods to the Follower interface without having to modify fields in the future, whereas you have no control over the Enum type, so if you decided that Followers needed a method, you would have to change the declaration in every place. This may never be the case with your scenario, but with so little cost to use this way, it's good defensive practice.

A second, slightly less important advantage, which is more about taste: it avoids the generics in the type which, when you include wildcards, can become less readable.

诗酒趁年少 2024-08-29 16:06:45
private Enum follower;

你可以让两个枚举实现相同的接口,例如Follower,并让字段为:

private Enum<? extends Follower> follower;

但你最好重新设计整个事情,这样感觉不太对劲。

private Enum follower;

And you can make both enums implement the same interface, for example Follower, and let the field be:

private Enum<? extends Follower> follower;

But you'd better redesign the whole thing, it doesn't feel right this way.

东风软 2024-08-29 16:06:45

您可以使用两个枚举的接口:

interface X {}

enum Connector implements X{
    AND, OR, XOR;
}

enum Component implements X{
    ACTIVITY
}

You can use an interface for both enums:

interface X {}

enum Connector implements X{
    AND, OR, XOR;
}

enum Component implements X{
    ACTIVITY
}
千紇 2024-08-29 16:06:45

我建议创建一个名为 Follower 的新类。即使可能,我也不确定单个变量是否应该有两种不同的类型。

我倾向于将枚举视为原始数据类型,而您的问题就像问如何使变量成为 int 或 long 。

I would suggest creating a new Class called Follower. I'm not sure a single variable should have two different types even if it's possible.

I tend to think of Enums as primitive data types and your question would be like asking how do I make a variable be either int or long.

一影成城 2024-08-29 16:06:45

为什么不拥有 java.lang.Enum 类型的追随者。它是 java 中所有枚举的父级。
所以下面的代码工作得很好。

包 com.test;

枚举 NewEnum {
一、二;
}

枚举另一个{
三、四;
}

公共类 TestMe {

static Enum num = NewEnum.one;

public static void main(String[] args) {
    System.out.println(num.toString());
    num = Another.three;
    System.out.println(num.toString());

}

<代码>}

Why not to have follower of type java.lang.Enum. It is parent of all enums in java.
So following code works just fine.

package com.test;

enum NewEnum {
one, two;
}

enum Another {
three, four;
}

public class TestMe {

static Enum num = NewEnum.one;

public static void main(String[] args) {
    System.out.println(num.toString());
    num = Another.three;
    System.out.println(num.toString());

}

}

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