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

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

问题描述

我想知道是否可以在 Java 中嵌套枚举.

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

这是我想要做的:

有一个由 CAT 和 DOG 组成的枚举物种,这将使我能够访问可用的 CAT 和 DOG 品种的子枚举.例如,我希望能够测试是猫还是狗,以及动物是 PERSAN CAT 还是 PITBULL DOG.CAT 和 DOG 品种必须是不同的枚举,即 CatBreeds 枚举和 DogBreeds 枚举.

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 :

物种:

  • 物种.CAT
  • 物种.DOG

品种:

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

推荐答案

用继承来做这件事不是更好吗?Cat 和 Dog 将是 Species 的子类.Persion 将是 Cat 的子类,Pitbull 将是 Dog 的子类.

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