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

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

问题描述

我有一个涉及嵌套枚举的数据结构,因此我可以执行以下操作:

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();

如果有方法声明:

someMethod(Drink type)
someOtherMethod(DrinkTypeInterface type)

那么我可以说(恰当地):

Then I could say (appropriately):

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

这是我想出来的:

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

和界面:

public interface DrinkTypeInterface {

    public String getLabel();
}

我想我只是想弄清楚在 Java 中执行此类事情的最佳方法是什么,或者我是否需要编写一堆 if 语句来处理各个 Drink.values().有什么帮助吗?

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();

首先,您提供的示例代码违反了德米特法则";有点 - 因为 COLUMBIAN 实例字段仅用于检索标签.此外,使用这种结构,COLUMBIAN 必须是 COFFEE 枚举的实例,但我认为这不是您真正想要的.

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)

我从你的样本中收集到的是,你想要一个包含组类型"的枚举.实际饮料是什么,然后每个人都有特定类型饮料的单独值.你的例子给出了 Coffee,但 Tea 也应该同样有效.

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.

问题在于您如何放置枚举.正如我之前所说,您必须使 COLUMBIAN 成为 COFFEE 枚举的实例,但这并不是构建它的最佳方式.

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.

问题是你有饮料,然后是咖啡/茶,然后是它们各自的类型.但是,仔细想想,虽然 HerbalTea 是一种茶,但它也是一种饮料——所以它不属于简单的 TEA 实例.

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());
    }
}

这个程序的输出如下:

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

那么,如果出于某种原因您不想将所有饮料都放在一个枚举中,那么我不明白您要做什么.在这种情况下,如果您确实具有跨越枚举的功能,请制作单独的 Coffee 和 Tea(以及其他)枚举并将接口应用于两个(或更多)枚举.但是,我想你是想像这样将它们分组.

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天全站免登陆