如何将枚举嵌套在枚举值的内部 [英] How to nest an Enum inside the value of an Enum

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

问题描述

我想知道Java是否可以嵌套枚举。



这里我想要做的是:



有一个枚举由CAT和DOG制成的物种将允许我访问可用的CAT和DOG品种的子枚举。例如,我想能够测试一下CAT或DOG,如果动物是PERSAN CAT或PITBULL DOG。 CAT和DOG品种必须是不同的枚举,即CatBreeds枚举和DogBreeds枚举。



以下是我想使用的访问模式的示例:



种类:




  • Species.CAT

  • 种类



品种:




  • Species.CAT.breeds.PERSAN

  • Species.DOG.breeds.PITBULL


解决方案

你不会更好地用继承做这个吗?猫和狗将是物种的子类。 Persion将是Cat的一个子类,Pitbull将是Dog的一个子类。



想要使用枚举是想实现什么?


I'd like to know if it is possible in Java to nest Enums.

Here what i'd like to be able to do :

Have an enum Species made of CAT and DOG wich would grant me access to sub enums of the available CAT and DOG breeds. For example, i'd like to be able to test if wether a CAT or a DOG and if an animal is a PERSAN CAT or a PITBULL DOG. CAT and DOG breeds must be distinct enums ,i.e a CatBreeds enum and a DogBreeds enum.

Here is an example of access pattern i'd like to use :

Species :

  • Species.CAT
  • Species.DOG

Breeds :

  • Species.CAT.breeds.PERSAN
  • Species.DOG.breeds.PITBULL

解决方案

Wouldn't you be better doing this with inheritance? Cat and Dog would be subclasses of Species. Persion would be a subclass of Cat and Pitbull would be a subclass of Dog.

What are trying to achieve by wanting to use Enums?

这篇关于如何将枚举嵌套在枚举值的内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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