多个枚举类在一个Java文件 [英] Multiple Enum Classes in one Java File

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

问题描述

我有一个常量3字符串数组。例如:

I have 3 String arrays with constants. eg:

 String[] digit = {"one", "two", "three"};
 String[] teen= {"ten", "twenty", "thirty"};
 String[] anchors = {"hundred", "thousand", "million"};

我想单独转让这些来枚举,所以我将有3枚举类:数字青少年的getValue 方法来实现。但是,我不想让他们在不同的文件,因为我只有小的数据和相同的数据类型。什么是有所有这些在同一个有意义的java文件访问方法的最佳方式?

I'm thinking of transferring these to enums separately, so I will have 3 enum classes: digit, teen and anchors with getValue methods implemented. But I don't want to have them in separate files as I have only small data and same type of data. What is the best way to have all these with access methods in same meaningful java file?

推荐答案

他们可以有三种内部类是这样的:

They can be three inner classes like this:

public class Types {
  public enum Digits {...}
  public enum Teens {...}
  ....
}

然后参考他们的 Types.Digits.ONE Types.Teen.TWENTY 等。

您也可以使用静态导入是这样的:

You can also use static imports like this:

import Types.Digits;
import Types.Teen;

..

为了有更短的引用: Digits.ONE Teen.TWENTY

in order to have shorter references: Digits.ONE, Teen.TWENTY etc.

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

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