任意 Java 枚举参数

发布于 2024-07-15 19:23:27 字数 770 浏览 2 评论 0原文

我对java相当陌生,并且习惯于枚举本质上只不过是一个命名的整数列表。

现在我正在编写一个实现,其中父类有几个采用枚举值作为参数的方法。 枚举将在子类中定义,并且会略有不同。 由于枚举基本上看起来像类,所以这并不像我预期的那样工作。 定义的每个枚举都将被视为不同类型的对象,父类必须选择定义的枚举之一作为参数。

有没有一种好方法可以让父类接受其子类中定义的任何枚举? 或者我必须为此编写一个自定义类?

编辑:这是我的示例,根据 Jon Skeets 的回答进行了修复,供以后研究如何执行此操作的任何人使用:

class Parent {
   protected interface ParentEvent {}
   private HashMap<ParentEvent, String> actions = new HashMap<ParentEvent, String>();

   protected void doStuff(ParentEvent e){

      if(actions.containsKey(e)){
         System.out.println(actions.get(e));
      }

   }
}

class Child extends Parent {
   enum Event implements ParentEvent {EDITED, ADDED, REMOVED}

   public void trigger(){
      doStuff(Event.REMOVED);
   }
}

I'm fairly new to java, and am used to enums essentially beeing nothing more than a named list of integers.

Now I'm writing an implementation where a parent class has a couple of methods that take an enum value as argument. The enum will be defined in child classes, and will differ slightly. Since enums basically seem to behave like classes, this doesn't work the way I expected it to. Each enum defined will be considered a different type of object and the parent class will have to pick one of the defined enums to take as argument.

Is there a good way to make the parent class accept any enum defined in it's child-classes? Or will I have to write a custom class for this?

Edit: Here is my example, fixed as per Jon Skeets answer, for anyone who is looking into how to do this later on:

class Parent {
   protected interface ParentEvent {}
   private HashMap<ParentEvent, String> actions = new HashMap<ParentEvent, String>();

   protected void doStuff(ParentEvent e){

      if(actions.containsKey(e)){
         System.out.println(actions.get(e));
      }

   }
}

class Child extends Parent {
   enum Event implements ParentEvent {EDITED, ADDED, REMOVED}

   public void trigger(){
      doStuff(Event.REMOVED);
   }
}

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

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

发布评论

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

评论(4

夜未央樱花落 2024-07-22 19:23:27

您可以让枚举实现一个接口,然后为您的父类方法提供该接口类型的参数。

正如你所说,Java 中的枚举有很大不同。 它们不是命名的数字 - 它们是一组固定的值,但这些值是面向对象的(即它们可以使用多态性等)。 Java 枚举几乎很稳定,除了初始化顺序方面的一些棘手问题。

You could make your enums implement an interface, then give your parent class method a parameter of that interface type.

As you say, enums are rather different in Java. They're not named numbers - they're a fixed set of values, but those values are object-oriented (i.e. they can use polymorphism etc). Java enums pretty much rock, except for a few tricksy issues around initialization ordering.

一曲琵琶半遮面シ 2024-07-22 19:23:27

如果我理解正确的话,您希望为枚举拥有一个公共基类,并希望为子类定义几个不相关的枚举集。 这对于 java 的类型安全枚举来说是不可能的,因为它们不允许您定义基类。

当然,仅仅定义一个枚举并始终扩展其值并不是一种选择,因为这显然违反了开闭原则。

对于这样的用例,我对 Josh Bloch 的类型安全枚举模式有相当好的经验,他在 Effective Java

只需在此处引入您的超类,并为您的客户端类所需的每个枚举值创建不同的子类。

if i understand you correctly, you want to have a common base class for your enum and want to define several unrelated sets of enums for the sub classes. This is not possible with java's typesafe enums, because they don't allow you to define a base class.

Of course it is not an option just to have one enum defined and always extend its values because this clearly violates the open close principle.

For such a use case I have fairly good experience with Josh Bloch's Typesafe Enum Pattern he describes in Effective Java

Just introduce your super class here and make distinct sub classes for each of enum values your client classes need.

淡看悲欢离合 2024-07-22 19:23:27

我不确定,但也许这就是您想要的:

public abstract class EnumTest<E extends Enum<E>> {
  public abstract void frobnicate(E value);
}

public class Derived extends EnumTest<Derived.DerivedEnum> {
  public void frobnicate(DerivedEnum value) {
    System.out.println(value);
  }

  public static enum DerivedEnum {
    FOO, BAR,
  }
}

I'm not sure, but maybe this is what you want:

public abstract class EnumTest<E extends Enum<E>> {
  public abstract void frobnicate(E value);
}

public class Derived extends EnumTest<Derived.DerivedEnum> {
  public void frobnicate(DerivedEnum value) {
    System.out.println(value);
  }

  public static enum DerivedEnum {
    FOO, BAR,
  }
}
像极了他 2024-07-22 19:23:27

如果枚举适用于不同的类,您可以在自己的文件中定义它们。 它们不需要嵌套在类中。

不过,您不能从一组枚举扩展另一组枚举。

我花了一段时间才摆脱枚举“只是一个整数”的心态。

You could define the enums in their own file if they're applicable to different classes. They don't need to be nested within a class.

You can't extend one set of enums from another though.

It took me a while to get out of the mindset of an enum 'just being an integer'.

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