是否可以迭代几个枚举类? [英] Is it possible to iterate through several enum classes?

查看:50
本文介绍了是否可以迭代几个枚举类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个枚举类.我想以某种方式将它们放在数组中,遍历数组并在每个枚举类中调用相同的方法.这在Java中可行吗?

I have three enum classes. I want to somehow put these in an array, loop through the array and call the same method in each enum class. Is this possible in Java?

在我看来,您不能将枚举类型放置在数组结构中(除非我错过了如何).

It seems to me you can't place enum types in an array structure (unless i've missed how).

谢谢.

推荐答案

让每种枚举类型实现一个通用接口,该接口具有要调用的 common方法.现在,您可以在迭代时将每个枚举转换为该公共接口并调用该方法.另请参阅 EnumSet

Let each enum type implement a common interface that has the common method that you want into invoke. You can now cast each enum, while iterating, to this common interface and call the method. Also look at EnumSet

这篇关于是否可以迭代几个枚举类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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