Java中的枚举基类

Java中的枚举基类,java,enums,subclass,Java,Enums,Subclass,使用它作为类枚举类的基类的最佳方法是什么。我希望能够创建不同的具体类型,而不必重新编写getter方法 public enum Tool extends Item { Pickaxe("Pickaxe", 101), Saw("Saw", 201); } getNames()将返回工具类中所有项目名称的列表 public enum Item { Default("Name", 0); private final int id; private fina

使用它作为类枚举类的基类的最佳方法是什么。我希望能够创建不同的具体类型,而不必重新编写getter方法

public enum Tool extends Item
{
    Pickaxe("Pickaxe", 101),
    Saw("Saw", 201);
}
getNames()将返回工具类中所有项目名称的列表

public enum Item
{
    Default("Name", 0);

    private final int id;
    private final String name;

    Item(String name, int id)
    {
        this.id = id;
        this.name = name;
    }

    public int getId()
    {
        return this.id;
    }

    public int[] getIds()
    {
        Item[] items = Item.values();
        int[] ids = new int[items.length];
        for (int i = 0; i < items.length; i++)
        {
            ids[i] = items[i].getId();
        }
        return ids;
    }

    public String getName()
    {
        return this.name;
    }

    public String[] getNames()
    {
        Item[] items = Item.values();
        String[] names = new String[items.length];
        for (int i = 0; i < items.length; i++)
        {
            names[i] = items[i].getName();
        }
        return names;
    }
}
公共枚举项
{
默认值(“名称”,0);
私有最终int id;
私有最终字符串名;
项(字符串名称,int-id)
{
this.id=id;
this.name=名称;
}
公共int getId()
{
返回此.id;
}
public int[]getIds()
{
Item[]items=Item.values();
int[]ids=新的int[items.length];
对于(int i=0;i

我知道这样是不可能的,但我该如何处理这种情况呢?我希望能够像访问enum:Tool.Pickaxe一样访问每个类的成员。

您必须自己实现,不能扩展已经实现的行为。但是,您可以强制自己或任何其他人实施各种方法:

public interface Item {
    public int getId();
    public String getName();
}

这是第一部分,现在您不希望通过枚举“实例”本身访问
getIds()
getNames()
,而是希望通过类作为静态函数访问它们

我希望这已经对您有所帮助,但它还远远不够完整,对于泛型,甚至可能有更简单的方法,但请务必理解枚举无法扩展任何内容。

您可以选择部分方式

public class MyItem extends Item<MyItem.Items> {
  public MyItem () {
    // Tell my parent class about my enum.
    super(EnumSet.allOf(Items.class));
  }
  public enum Items implements Item.Items {
    Pickaxe("Pickaxe", 101),
    Saw("Saw", 201);
    private final int id;
    private final String name;

    // You have to do these.
    Items(String name, int id) {
      this.id = id;
      this.name = name;
    }

    @Override
    public int getId() {
      return this.id;
    }

    @Override
    public String getName() {
      return this.name;
    }

  }
}

// The parent class that does the common work.
public class Item<I extends Enum<I> & Item.Items> {
  // Keep track of the items.
  private final Set<I> items;

  // Pas a set of the items in the constructor.
  public Item(Set<I> items) {
    this.items = items;
  }

  // The interface.
  public interface Items {
    public int getId();

    public String getName();

  }

  // Ready-made functionality.
  public List<Integer> getIds() {
    List<Integer> ids = new ArrayList<>();
    for (I i : items) {
      ids.add(i.getId());
    }
    return ids;
  }

  public List<String> getNames() {
    List<String> names = new ArrayList<>();
    for (I i : items) {
      names.add(i.getName());
    }
    return names;
  }

}
公共类MyItem扩展了项目{
公共MyItem(){
//告诉我的父类我的枚举。
super(EnumSet.allOf(Items.class));
}
公共枚举项实现项。项{
鹤嘴锄(“鹤嘴锄”,101),
锯(“锯”,201);
私有最终int id;
私有最终字符串名;
//你必须这样做。
项目(字符串名称、整数id){
this.id=id;
this.name=名称;
}
@凌驾
公共int getId(){
返回此.id;
}
@凌驾
公共字符串getName(){
返回此.name;
}
}
}
//执行公共工作的父类。
公共类项目{
//跟踪项目。
私人最终设置项目;
//Pas是构造函数中的一组项。
公共项目(成套项目){
这个项目=项目;
}
//界面。
公共接口项{
public int getId();
公共字符串getName();
}
//现成的功能。
公共列表getIds(){
列表ID=新的ArrayList();
(I:项目){
add(i.getId());
}
返回ID;
}
公共列表getNames(){
列表名称=新的ArrayList();
(I:项目){
name.add(i.getName());
}
返回姓名;
}
}
在这里,您仍然需要让枚举构造函数存储字段,但您只需要实现一个接口。然后,父类可以在构造时对给定的集合执行所有工作


您当然可以使用此技术在父类中移出大量代码,但遗憾的是并非全部。

一个提示:枚举无法扩展另一个类,但它们仍然可以实现接口。枚举自动扩展类
Enum
。它们不能扩展类。使用接口。实现接口意味着我仍然需要为每个子类编写方法代码。我想以某种方式避免这种情况。@B-Fir3我自己也遇到过这个问题。我最终通过编写一个程序来修复它,该程序基于一些输入为枚举创建*.java文件。在您的示例中,以
Name,Id
的形式在每一行上进行一些输入已经足够了。但如果可能的话,您应该避免创建另一个程序(依赖性),并尝试通过代码解决它。
public class MyItem extends Item<MyItem.Items> {
  public MyItem () {
    // Tell my parent class about my enum.
    super(EnumSet.allOf(Items.class));
  }
  public enum Items implements Item.Items {
    Pickaxe("Pickaxe", 101),
    Saw("Saw", 201);
    private final int id;
    private final String name;

    // You have to do these.
    Items(String name, int id) {
      this.id = id;
      this.name = name;
    }

    @Override
    public int getId() {
      return this.id;
    }

    @Override
    public String getName() {
      return this.name;
    }

  }
}

// The parent class that does the common work.
public class Item<I extends Enum<I> & Item.Items> {
  // Keep track of the items.
  private final Set<I> items;

  // Pas a set of the items in the constructor.
  public Item(Set<I> items) {
    this.items = items;
  }

  // The interface.
  public interface Items {
    public int getId();

    public String getName();

  }

  // Ready-made functionality.
  public List<Integer> getIds() {
    List<Integer> ids = new ArrayList<>();
    for (I i : items) {
      ids.add(i.getId());
    }
    return ids;
  }

  public List<String> getNames() {
    List<String> names = new ArrayList<>();
    for (I i : items) {
      names.add(i.getName());
    }
    return names;
  }

}