簡體   English   中英

使用通用類型Enum創建EnumMap

[英]Creating an EnumMap with a generic type Enum

如果我有一堆包含Enum和EnumMap的類,我想為這些類創建一個超類。

public interface ColorEnum {
}

class ColorMarbles extends Toy {
    enum MARBLE implements ColorEnum
        { BLUE, GREEN }
    EnumMap<MARBLE, String> names = new EnumMap<MARBLE, String>(MARBLE.class);
    //stuff
    // fields

    public void populate(ArrayList<String> designer) {
        int i = 0;
        for(MARBLE marble : MARBLE.values()) {
            marble.name = designer.get(i);
            i++;
        }
    }
}

class ColorBalloons extends Toy {
    enum BALLOON implements ColorEnum
        { YELLOW, RED }
    EnumMap<BALLOON, String> names = new EnumMap<BALLOON, String>(BALLOON.class);
    //stuff
    // fields

    public void populate(ArrayList<String> designer) {
        int i = 0;
        for(BALLOON balloon : BALLOON.values()) {
            balloon.name = designer.get(i);
            i++;
        }
    }
}

如何創建一個超類,使其包含一個包含類似ColorEnum類型的枚舉的通用EnumMap?

public abstract class Toy {
    EnumMap<ColorEnum, String> names;
}

eidt:我意識到我的例子太模糊了。 狗可能是一個壞榜樣。 我把它改成希望更清楚的東西。

我所擁有的是一堆類,其中包含填充EnumMap的populate等方法。 名稱按預定義順序排列。 我不希望在每個類中定義填充,而是希望能夠將它帶到Toy超類中,因此我不必在每個新類類型的Toy中保留復制粘貼。

希望這能解釋更多我正在尋找的東西。

我覺得你的設計不必要地過於復雜。

隨着枚舉

如果您不需要類繼承,則可以像使用頂級類一樣直接使用枚舉。

public interface Animal {}

public enum Dog implements Animal {
    HUSKY("Husky"), LAB("Labrador");

    private final String name;

    Dog(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

枚舉可以像任何其他Java類一樣聲明字段,方法和實現接口。 他們唯一的限制是他們的直接超類總是java.lang.Enum並且它們不能被擴展。

但是,每個枚舉常量都可以將自己的一組唯一數據傳遞給它的構造函數。 甚至可能每個常量都可以通過其獨特的實現覆蓋該枚舉的常用方法。

一個很好的教程,解釋更多關於枚舉的全部功能: http//javarevisited.blogspot.cz/2011/08/enum-in-java-example-tutorial.html


沒有枚舉

如果你需要一個實際的類繼承來共享一些常見的方法(例如來自Animal超類),我仍然會放棄map方法,而是嘗試更多面向OOP的方法:

public class Animal {
}

public abstract class Dog extends Animal {

    public abstract String getName();

    public static class Husky extends Dog {
        @Override
        public String getName() {
            return "husky";
        }
    }

    public static class Lab extends Dog {
        @Override
        public String getName() {
            return "labrador";
        }
    }

}

我用過這種方法的一種機制是擴展一個通用基類,它具有一個通用參數,允許你將Enum細節傳遞給它。

此示例為數據庫表定義基本Table類:

public class Table<Column extends Enum<? extends Column>> {
  // Name of the table.
  protected final String tableName;
  // All of the columns in the table. This is actually an EnumSet so very efficient.
  protected final Set<Column> columns;

  /**
   * The base interface for all Column enums.
   */
  public interface Columns {
    // What type does it have in the database?
    public Type getType();
  }

  // Small list of database types.
  public enum Type {
    String, Number, Date;
  }

  public Table(String tableName,
               Set<Column> columns) {
    this.tableName = tableName;
    this.columns = columns;
  }

}

現在你可以繼承這個:

public class VersionTable extends Table<VersionTable.Column> {

  public enum Column implements Table.Columns {
    Version(Table.Type.String),
    ReleaseDate(Table.Type.Date);

    // Sadly all of this must be in ALL of your enums but most of the work can be pushed up to `Table`
    final Table.Type type;

    Column(Table.Type type) {
      this.type = type;
    }

    @Override
    public Type getType() {
      return type;
    }
  }

  public VersionTable() {
    super("Versions", EnumSet.allOf(Column.class));
  }
}

並使用處理枚舉的父類中的功能。

請注意,我將EnumSet傳遞給Table構造函數。 如果您確定EnumSet不足,我相信您可以更改此項以適應您的EnumMap要求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM