比较枚举条目 - Java

时间:2017-04-09 15:54:14

标签: java inheritance enums

我们说我有几台机器:

//Test code of course.
public class Start{
  public static void main(String args[]){
    System.out.println(Machine.COFFEE_GRINDER.getCatalogId());
    System.out.println(Machine.COFFEE_MACHINE.isOfType(Machine.BASIC_MACHINE)); 
    //The above should be true.
  }

  private enum Machine {
    BASIC_MACHINE (-1),
    BEVERAGE (-1),
    COFFEE(-1),
    COFFEE_GRINDER (5),
    COFFEE_MACHINE (6),
    GARDEN (-1),
    LAWN_MOWER (28);

    private final int catalogId;

    public int getCatalogId(){
      return catalogId;
    }

    public boolean isOfType(Machine to){
      return this == to;
    }

    Machine (int catalogId) {
      this.catalogId = catalogId;
    }
  }
}

在上面的示例中,有一些机器,因为它们会出现在目录中,并且具有与之关联的ID号。还有部分和部分。所以BEVERAGE机器仍然是BASIC_MACHINE。 COFFEE机器仍然是BEVERAGE机器。

程序中的一些功能在执行功能之前必须检查机器是否实际上是BEFERAGE机器。目录中的COFFEE_GRINDER和COFFEE_MACHINE都会检出并且该功能应该通过。

我正在寻找的行为与抽象类的instanceof或继承相当。无论如何,COFFEE_MACHINE是一种BASIC_MACHINE,我想检查一下。

所以:

Machine.COFFEE_MACHINE isa Machine.COFFEE
Machine.BEVERAGE isa MACHINE.BASIC_MACHINE
Machine.LAWN_MOWER isa Machine.GARDEN == Machine.BASIC_MACHINE
Machine.COFFEE_MACHINE isnota Machine.COFFEE_GRINDER
Machine.LAWN_MOWER isnota Machine.COFFEE

2 个答案:

答案 0 :(得分:2)

一种可能的解决方案是使用回调和保存的超类型来模仿某些继承。 我很好奇这是否是最佳方式。

实现这将是这样的:

//Test code of course.
public class Start{
  public static void main(String args[]){
    System.out.println(Machine.COFFEE_GRINDER.getCatalogId());
    System.out.println(Machine.COFFEE_MACHINE.isOfType(Machine.BASIC_MACHINE)); //Should be true.
  }

  private enum Machine {
    BASIC_MACHINE (-1),
    BEVERAGE (-1, BASIC_MACHINE),
    COFFEE(-1, BEVERAGE),
    COFFEE_GRINDER (5, COFFEE),
    COFFEE_MACHINE (6, COFFEE),
    GARDEN (-1, BASIC_MACHINE),
    LAWN_MOWER (28, GARDEN);

    private int catalogId;
    private Machine superMachine;

    public int getCatalogId(){
      return catalogId;
    }

    public Machine getSuperMachine(){
      return superMachine;
    }

    //With callback to superMachine (if present)
    public boolean isOfType(Machine to){
      return this == to || (getSuperMachine() != null && getSuperMachine().isOfType(to));
    }

    Machine (int catalogId) {
      this.catalogId = catalogId;
    }

    Machine (int catalogId, Machine superMachine) {
      this(catalogId);
      this.superMachine = superMachine;
    }
  }
}

答案 1 :(得分:0)

如果我正确理解了您的目标,您希望表达enum常量之间的关系网络。考虑到继承不一定与您希望表达的关系一致。继承模型类型之间的is-a关系;您希望实例之间存在connects-to关系。也许使用不同于“超级/继承”的术语,例如“所有者拥有”。您可以在每个常量中嵌入指向owner实例的指针,并在静态初始化程序和构造函数中创建非循环有向图结构。

public enum NetworkedMachine
{
BASIC_MACHINE(-1, null),
BEVERAGE(-1, BASIC_MACHINE),
COFFEE(-1, BASIC_MACHINE),
COFFEE_GRINDER(5, COFFEE),
COFFEE_MACHINE(6, COFFEE),
GARDEN(-1, BASIC_MACHINE),
LAWN_MOWER(28, GARDEN),
;

static final Map<NetworkedMachine, Set<NetworkedMachine>> owners;
static {
  Map<NetworkedMachine, Set<NetworkedMachine>> ownership = new HashMap<>();
  for (NetworkedMachine machine : values()) {
    ownership.put(machine, new HashSet<>());
  }
  for (NetworkedMachine machine : values()) {
    if (machine.owner != null) {
      ownership.get(machine.owner).add(machine);
    }
  }
  for (NetworkedMachine machine : values()) {
    Set<NetworkedMachine> owns = ownership.get(machine);
    ownership.put(machine, Collections.unmodifiableSet(owned));
  }
  owners = Collections.unmodifiableMap(ownership);
}

private final int catalogId;
private final NetworkedMachine owner;

NetworkedMachine(int catalogId, NetworkedMachine machine) {
  this.catalogId = catalogId;
  this.owner = machine;
}

public int getCatalogId() {
  return catalogId;
}

public NetworkedMachine getOwner() {
  return owner;
}

public Set<NetworkedMachine> getOwns() {
  return owners.get(this);
}

public boolean isOwned() {
  return owner != null;
}

}

测试输出:

BASIC_MACHINE id: -1, owner: [null], owns: [GARDEN, BEVERAGE, COFFEE]
BEVERAGE id: -1, owner: [BASIC_MACHINE], owns: []
COFFEE id: -1, owner: [BASIC_MACHINE], owns: [COFFEE_GRINDER, COFFEE_MACHINE]
COFFEE_GRINDER id: 5, owner: [COFFEE], owns: []
COFFEE_MACHINE id: 6, owner: [COFFEE], owns: []
GARDEN id: -1, owner: [BASIC_MACHINE], owns: [LAWN_MOWER]
LAWN_MOWER id: 28, owner: [GARDEN], owns: []