如何在一个类中编写多个枚举类 [英] How to write multiple enum classes in a class

查看:883
本文介绍了如何在一个类中编写多个枚举类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在Java类中编写多个枚举的问题.我们可以这样吗?

I have a question on writing multiple enums in a java class. Can we have like this ?

能否请您帮我完成此操作?

Could you please help me on how to complete this ?

例如:

public final class Test{

    public enum Unit{
        HORIZONTAL("HORIZONTAL");

    }

    public enum Code {
        COMPANY ("COMPANY");
    }

    public enum Version{
        ONE(1)

    }
}

推荐答案

Java中的枚举定义是一个类.该语言提供了枚举特定的语法功能,但是就JVM而言,枚举是一个类.

An enum definition in Java is a class. The language provides the syntactic features that are particular to enums, but as far as the JVM is concerned, the enum is a class.

实际上,声明了一个枚举:

In fact, an enum that is declared:

public enum MyConstants{ ... }

在JVM上实现为:

public static class Enum<MyConstants> { ... }

因此,声明枚举类的规则与声明类的规则相同..java文件可能只有一个公共类.

The rules for declaring enum classes is thus the same as for declaring classes. A .java file may have only one public class.

因此,您只能在.java文件中声明一个公共枚举.您可以声明任意数量的程序包专用枚举.

You can therefore declare only one public enum in a .java file. You may declare any number of package-private enums.

如果您有多个紧密相关的枚举,则将它们全部声明为封闭类的嵌套成员没有什么害处:

If you have multiple, closely related enums, there is no harm in declaring them all as nested members of an enclosing class:

public class MyEnumContainer {

    static enum MyEnum1 { ... }

    static enum MyEnum2 { ... }

    :
    :
}

在这种情况下,您将需要导入MyEnclosingClass,或者将常量引用为MyEnclosingClass.MyEnum1.ENUM_ONE.请注意,所有枚举都是静态类,因此使用static关键字不会对您造成不利影响,但这不是必需的.

In this case, you would need to import your MyEnclosingClass, or refer to the constants as MyEnclosingClass.MyEnum1.ENUM_ONE, for example. Note that all enums are static classes, so using the static keyword doesn't penalize you, but it isn't necessary.

每个成员枚举定义必须独立(即,它必须是完整的枚举定义).如果您有与常量相关的数据,则每个枚举定义将需要其自己的实例字段和构造函数.如果您有特定于常量的方法,则每个枚举都需要提供这些方法.

Each of the member enum definitions must stand alone (i.e., it must be a complete enum definition). If you have constant-associated data, then each enum definition will need its own instance fields and constructor. If you have constant-specific methods, each enum will need to provide the methods.

如果您担心大量的代码重复,则可以将通用代码放入私有的静态帮助器类中,然后将方法调用转发给枚举定义中的那些.

If you are concerned about a lot of code duplication, you can put the common code into a private static helper class, and then forward method calls to those within your enum definitions.

使用您的枚举定义,嵌套的枚举应类似于:

Using your enum definitions, the nested enums should look something like:

public final class Test{

    public enum Unit {
         HORIZONTAL ("HORIZONTAL"),
         VERTICAL   ("VERTICAL"),
         :
         :
         DIAGONAL   ("DIAGONAL");

         private final String e_name;

         Unit(String name)
             { this.e_name = name; }

         public String getName() { return e_name; }
    }

    public enum Code {
    :
    :
    }

    public enum Version{
    :
    :
    }
}

这篇关于如何在一个类中编写多个枚举类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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