java如何使包含枚举列表的枚举常量?
最近我一直在尝试制作一个相对论能量计算器。我用List<ComponentType>
来表示构建在ComponentType.ELECTRON
、ComponentType.PROTON
和ComponentType.NEUTRON
上的任何粒子,其中ComponentType
是包含enum
的质量
后来我想创建一个ParticleFactory
类,它将对应于GUI下拉列表中的基本粒子列表。比如:ParticleFactory(Hydrogen)
会给我一个包含{ComponentType.ELECTRON, ComponentType.PROTON}
列表的粒子对象。这将使修改变得容易,因为ParticleFactory
和GUI将对基本粒子使用相同的enum
当我试图实现它时,问题出现了。我知道它不可能使用不正确的语法,但无论我做什么,我总是会出错
public enum GenericParticleType {
ELECTRON({"electron","e"}, new ArrayList<ComponentType>());
private final String[] names;
private final List<ComponentType> components;
GenericParticleType(String[] names, List<ComponentType> components) {
this.names = names;
this.components = components;
}
}
目标是使enum
包含其他enums
的列表:有没有合适的方法
有没有更好的办法?常数HashMap
# 1 楼答案
我认为这没有问题:
# 2 楼答案
我认为您需要以下设计:
GenericParticle
的接口李>您可以通过以下代码实现这一点:
用作:
您确实需要针对以下事实对您的电视机采取预防措施:
GenericParticle
,但是为此,您有文档来确保他做了正确的事情李>