public class Enumvalues{
enum courseList {
JAVA,
C,
PYTHON,
PERL
}
enum generalInformation {
NAME,
AGE,
PHONE
}
enum sex {
MALE,
FEMALE
}
}
public static void main(String args[]) {
printEnumValue(generalInformation); // how to pass enum in this block
}
static void printEnumValue(enum generalInformation) { // how to receive enum in this block
System.out.println(java.util.Arrays.asList(generalInformation.values()));
}
回答
如果你想从
public class Test {
enum GeneralInformation{
NAME;
}
private static void print(GeneralInformation val){
System.out.println(val);
}
public static void main(String[] args) {
print(GeneralInformation.NAME);
}
}
否则,如果你想传递全班那么,因为它不是从问题明确枚举传递一个值
private static void print(Class<?> generalInfo){
}
public static void main(String[] args) {
print(GeneralInformation.class);
}
的枚举使用就像一个类型:
static void printEnumValue(courseList generalInformation) { // how to receive enum in this block
使用像:
printEnumValue(courseList.JAVA);
printEnumValue(EnumValues.generalInformation.NAME,EnumValues.generalInformation.AGE,EnumValues.generalInformation.PHONE);
接收:
public static void printEnumValue(EnumValues.generalInformation en){
System.out.println(en.toString());
}
枚举是一个类。所以你可以传递一个类的实例(例如EnumValues.generalInformation.PHONE
),或者你可以传递类本身(例如EnumValues.generalInformation.class
)。
如果要列出枚举类的所有实例,则需要传递枚举类,例如使用EnumSet.allOf(EnumValues.generalInformation.class)
。
您的困惑主要来自您不尊重Java命名约定的事实。 ENums是类,应以大写字母开头(例如,GeneralInformation
)。另一个混淆之处是名称的选择不当。 JAVA不是课程列表。这是一门课程。所以枚举应该被命名为Course
。
感谢您的信息 –
请注意,您的问题混淆了两个不同的问题:将枚举传递给函数或将枚举常量传递给函数。我的理解是你想要传递枚举本身,而不是函数的常量之一。如果不是,请参阅Narendra Pathai关于如何将单个枚举常量传递给函数的答案。如果你不知道一个枚举和枚举常量之间的区别是什么,查看docs约枚举...
我知道你想要的是有一个打印(或任何其他)函数,其中您可以传递任何可能的枚举,以打印每个枚举的可能值(即常量)。我发现下面的两种方法做到这一点:
比方说我们有以下枚举:
// The test enum, any other will work too
public static enum ETest
{
PRINT,MY,VALUES
}
变式1:传递从枚举到你的函数常量数组;由于枚举的常量是静态值,就可以很容易地访问,并传递给你的“打印”功能如下:
public static void main(String[] args)
{
// retreive all constants of your enum by YourEnum.values()
// then pass them to your function
printEnum(ETest.values());
}
// print function: type safe for Enum values
public static <T extends Enum<T>> void printEnum(T[] aValues)
{
System.out.println(java.util.Arrays.asList(aValues));
}
变式2:传递枚举的类的功能参数。这可能看起来更漂亮,但要注意,反射参与(性能):
public static void main(String[] args)
{
printEnum2(ETest.class);
}
// print function: accepts enum class and prints all constants
public static <T extends Enum<T>> void printEnum2(Class<T> aEnum)
{
// retreive all constants of your enum (reflection!!)
System.out.println(java.util.Arrays.asList(aEnum.getEnumConstants()));
}
在我看来,最好的变式使用变式1,因为反射的过度使用的2.变式2唯一的好处给你,是你的函数内部有Enum本身的Class对象(静态枚举,不仅是常量),所以我已经提到它的完整性。
- 1. Java:将枚举传递给方法
- 2. 如何将枚举作为参数传递给函数?
- 3. 将枚举值作为参数传递
- 4. 将C#枚举传递给C++ CLI的包装方法参数
- 5. 将“未知枚举”传递给方法
- 6. 将枚举传递给方法
- 7. 如何将简单(枚举)参数传递给C#中的类?
- 8. 传递一个枚举方法参数
- 9. 传递枚举作为参数
- 10. 将类作为参数传递给java中的方法
- 11. 我怎样才能将枚举对象与嵌套枚举传递给方法作为参数?
- 12. 如何将Scala枚举作为参数传递?
- 13. 将数组作为参数传递给枚举构造函数:语法问题
- 14. 如何在java方法中将数组作为参数传递?
- 15. 如何将新数组作为参数传递给Java中的方法?
- 16. 在Java中传递枚举类型值作为参数
- 17. 传递枚举类型作为参数在Java中
- 18. 将参数传递给Java方法
- 19. 将枚举参数传递给case类不起作用
- 20. 如何将数组作为分隔参数传递给方法
- 21. 如何将GWT方法作为参数传递给Javascript函数?
- 22. 是否可以将非枚举值作为枚举函数参数传递?
- 23. Java的验证作为参数传递一个枚举值
- 24. 如何在Java中将方法作为参数传递?
- 25. 传递枚举作为参数 - 作为字典(的字符串,枚举)(VB 2012)
- 26. 将Java类作为参数传递给JRuby方法
- 27. 如何将数组传递给枚举中的构造函数?
- 28. 将C#中的十六进制枚举参数传递给C
- 29. 将特定枚举值传递给objective-c中的方法
- 30. 将枚举值传递给Powershell中ComObject的方法
我想要通过enum.generalInformation –
全班作为枚举就像任何其他类一样,你想要类的文字?然后我编辑使用'类>' –
或者如果你想要枚举类的所有实例,然后使用'GeneralInformation.values()'这将给你GeneralInformation [],其中包括枚举的所有实例 –