2017-04-09 90 views
0

比方說,我有幾臺機器:比較枚舉條目 - Java的

//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。一臺咖啡機仍然是一臺飲料機。

程序中的一些函數在執行其功能之前,必須檢查機器是否實際上是一臺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 
+1

你只需要遍歷從最專業的'enum'值開始的繼承路徑,並檢查每一步。聽起來很微不足道。 – Jack

+0

'VENDING'未定義。你的意思是'飲料'嗎? – Andreas

+0

您不能指定'Machine.COFFEE_MACHINE = Machine.COFFEE'等,因爲枚舉常量是常量。 「實現某些東西?」如果你能描述你想要完成的事情,這將會有所幫助。標題提到了繼承,但除了匿名體類之外,你不能繼承一個枚舉類型,也沒有說什麼繼承與任何東西有關。你不需要「檢查類型」就可以知道每個常量,它是__個對象,是'Machine'類型的。如圖所示,您的代碼不會編譯。請澄清你想要的,並提供可編譯的代碼。 –

回答

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; 
    } 
    } 
} 
0

如果我理解你的目標正確,你想表達的enum常數之間的關係網絡。考慮繼承不一定與你想表達的關係一致。繼承模型類型之間的is-a關係;你想要一個connects-to實例之間的關係。也許使用與「super/inherits」不同的術語,比如「owner-owns」。您可以在每個常量中嵌入一個指向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: []