在Java中使用嵌套的枚举类型 [英] Using nested enum types in Java

查看:286
本文介绍了在Java中使用嵌套的枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据结构,涉及嵌套的枚举,这样我可以做一些类似如下的内容:

 饮料.COFFEE.getGroupName(); 
Drink.COFFEE.COLUMBIAN.getLabel();

如果有方法声明:

  someMethod(饮料类型)
someOtherMethod(DrinkTypeInterface type)

然后我可以说(适当):

  someMethod(Drink.COFFEE)
someOtherMethod(Drink.COFFEE .COLUMBIAN)

这是我想出的:

  public enum Drink {

COFFEE(Coffee);

private String groupName;

private Drink(String groupName){
this.groupName = groupName;
}

public enum咖啡工具DrinkTypeInterface {

COLUMBIAN(Columbian Blend),
ETHIOPIAN(埃塞俄比亚混合);

private String label;

私人咖啡(String label){
this.label = label;
}

public String getLabel(){
return this.label;
}
}

String getGroupName(){
return this.groupName;
}
}

接口:

  public interface DrinkTypeInterface {

public String getLabel();
}

我想我只是想围绕最好的方式做这样的事情是在Java中,或者如果我需要编写一堆if-statements来处理个人的Drink.values()。任何帮助?

解决方案

  Drink.COFFEE.getGroupName(); 
Drink.COFFEE.COLUMBIAN.getLabel();

首先,您提供的示例代码有些违反了除法器的规定 - 像哥伦比亚大学的实例字段仅用于检索标签。另外,使用这种结构,COLUMBIAN 必须是COFFEE枚举的一个实例,但我不认为这是你真正要去的这个。

  someMethod(饮料类型)
someOtherMethod(DrinkTypeInterface type)

someMethod(Drink.COFFEE)
someOtherMethod(Drink。 COFFEE.COLUMBIAN)

从您的样本收集的是您想要的包含实际饮料是什么的组型的列举,然后每个具有特定类型的饮料的个体价值。你的例子给了咖啡,但茶也应该是一样的。



问题是你如何放置你的枚举。正如我之前所说,你必须让哥伦布成为COFFEE枚举的实例,但这并不是最好的结构方式。



问题是你喝了咖啡,然后喝咖啡,然后喝了咖啡。
但是,如果你想到,虽然草药茶是一种茶,但它也是一种饮料 - 所以它不属于TEA的一个实例。



但是,如果您将饮料类型一个枚举,您可以获得所需的内容,结构变得更加清晰。由于接口和授权的权力,饮料类型和饮料枚举都可以以相同的方式进行处理,如以下示例程序:

  public final class DrinkEnumExample {

public interface DrinkTypeInterface {

String getDisplayableType();
}

public static enum DrinkType实现DrinkTypeInterface {

COFFEE(Coffee),TEA(Tea);
private final String type;

private DrinkType(final String type){
this.type = type;
}

public String getDisplayableType(){
return type;
}
}

public static enum Drink implements DrinkTypeInterface {

COLUMBIAN(Columbian Blend,DrinkType.COFFEE),
ETHIOPIAN (埃塞俄比亚混合,DrinkType.COFFEE),
MINT_TEA(Mint,DrinkType.TEA),
HERBAL_TEA(Herbal,DrinkType.TEA),
EARL_GREY ,DrinkType.TEA);
private final String label;
私人终端DrinkType类型;

private Drink(String label,Dri​​nkType type){
this.label = label;
this.type = type;
}

public String getDisplayableType(){
return type.getDisplayableType();
}

public String getLabel(){
return label;
}
}

public DrinkEnumExample(){
super();
}

public static void main(String [] args){
System.out.println(所有饮料类型);
(DrinkType类型:DrinkType.values()){
displayType(type);
System.out.println();
}
System.out.println(所有饮料);
(喝酒:Drink.values()){
displayDrink(drink);
System.out.println();
}
}

私人静态void displayDrink(喝酒){
displayType(drink);
System.out.print( - );
System.out.print(drink.getLabel());
}

private static void displayType(DrinkTypeInterface displayable){
System.out.print(displayable.getDisplayableType());
}
}

该程序的输出如下: p>

 所有饮料类型
咖啡

所有饮料
咖啡 - 哥伦比亚混合
咖啡 - 埃塞俄比亚混合
茶 - 薄荷
茶 - 草药
茶 - 伯爵灰色

现在,如果由于某种原因,您不想在一个枚举中使用所有饮料,那么我不明白你要做什么。在这种情况下,如果您确实具有跨越枚举的功能,请单独使用Coffee和Tea(和任何)枚举,并在两个(或多个)枚举上应用该界面。但是,我想你正在试图将它们分组。


I have a data structure in mind that involves nested enums, such that I could do something like the following:

Drink.COFFEE.getGroupName();
Drink.COFFEE.COLUMBIAN.getLabel();

And if there were method declarations:

someMethod(Drink type)
someOtherMethod(DrinkTypeInterface type)

Then I could say (appropriately):

someMethod(Drink.COFFEE)
someOtherMethod(Drink.COFFEE.COLUMBIAN)

This is what I came up with:

public enum Drink {

    COFFEE("Coffee");

    private String groupName;

    private Drink(String groupName) {
        this.groupName = groupName;
    }

    public enum Coffee implements DrinkTypeInterface {

        COLUMBIAN("Columbian Blend"),
        ETHIOPIAN("Ethiopian Blend");

        private String label;

        private Coffee(String label) {
            this.label = label;
        }

        public String getLabel() {
            return this.label;
        }
    }

    String getGroupName() {
        return this.groupName;
    }
}

And the interface:

public interface DrinkTypeInterface {

    public String getLabel();
}

I think I'm just trying to wrap my head around what the best way to do this sort of thing is in Java, or if I need to write a bunch of if-statements to deal with the individual Drink.values(). Any help?

解决方案

Drink.COFFEE.getGroupName();
Drink.COFFEE.COLUMBIAN.getLabel();

First off, that sample code you gave violates the "law of demeter" somewhat - as the COLUMBIAN instance field is only used to retrieve the label. Also, with that structure, COLUMBIAN has to be an instance of the COFFEE enum, but I don't think that's what you're really going for here.

someMethod(Drink type)
someOtherMethod(DrinkTypeInterface type)

someMethod(Drink.COFFEE)
someOtherMethod(Drink.COFFEE.COLUMBIAN)

What I'm gathering from what your sample is, is that you want to have an enumeration that contains a "group type" of what the actual drink is, and then each one has individual values for the specific type of drink. Your example gives Coffee, but Tea should work just as well.

The problem is how you've placed your enumerations. As I said before, you'd have to make COLUMBIAN an INSTANCE of the COFFEE enumeration, but that's not really the best way to structure this.

The problem is that you've got Drink, then Coffee/Tea, and then their individual types. But, if you think about it, although HerbalTea IS A Tea, it is also a DRINK - so it doesn't belong as simply an instance of a TEA.

But, if you make the drink type an enum in and of itself, you get what you wanted, and the structure becomes clearer. And due to interfaces and the power of delegation, both the drink type and the drink enum can be processed in the same manner, as with the following example program:

public final class DrinkEnumExample {

    public interface DrinkTypeInterface {

        String getDisplayableType();
    }

    public static enum DrinkType implements DrinkTypeInterface {

        COFFEE("Coffee"), TEA("Tea");
        private final String type;

        private DrinkType(final String type) {
            this.type = type;
        }

        public String getDisplayableType() {
            return type;
        }
    }

    public static enum Drink implements DrinkTypeInterface {

        COLUMBIAN("Columbian Blend", DrinkType.COFFEE),
        ETHIOPIAN("Ethiopian Blend", DrinkType.COFFEE),
        MINT_TEA("Mint", DrinkType.TEA),
        HERBAL_TEA("Herbal", DrinkType.TEA),
        EARL_GREY("Earl Grey", DrinkType.TEA);
        private final String label;
        private final DrinkType type;

        private Drink(String label, DrinkType type) {
            this.label = label;
            this.type = type;
        }

        public String getDisplayableType() {
            return type.getDisplayableType();
        }

        public String getLabel() {
            return label;
        }
    }

    public DrinkEnumExample() {
        super();
    }

    public static void main(String[] args) {
        System.out.println("All drink types");
        for (DrinkType type : DrinkType.values()) {
            displayType(type);
            System.out.println();
        }
        System.out.println("All drinks");
        for (Drink drink : Drink.values()) {
            displayDrink(drink);
            System.out.println();
        }
    }

    private static void displayDrink(Drink drink) {
        displayType(drink);
        System.out.print(" - ");
        System.out.print(drink.getLabel());
    }

    private static void displayType(DrinkTypeInterface displayable) {
        System.out.print(displayable.getDisplayableType());
    }
}

The output of this program is as follows:

All drink types 
Coffee 
Tea 
All drinks 
Coffee - Columbian Blend 
Coffee - Ethiopian Blend
Tea - Mint 
Tea - Herbal 
Tea - Earl Grey

Now then, if for some reason you didn't want all your drinks in a single enum, then I didn't understand what you were going for. In that case, if you do have functionality that spans the enums, make separate Coffee and Tea (and whatever) enumerations and apply the interface on both (or more) enumerations. But, I think you were trying to group them like this.

这篇关于在Java中使用嵌套的枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆