如何将enum作为参数传递给java中的方法?

Ree*_*nda 29 java enums

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()));
}
Run Code Online (Sandbox Code Playgroud)

JB *_*zet 17

枚举是一个类.因此,您可以传递类的实例(EnumValues.generalInformation.PHONE例如),或者您可以传递类本身(EnumValues.generalInformation.class例如).

如果要列出枚举类的所有实例,则需要传递枚举类,并使用EnumSet.allOf(EnumValues.generalInformation.class)例如.

您的困惑主要来自于您不尊重Java命名约定.ENums是类,应该以大写字母开头(GeneralInformation例如).另一个混乱的来源是名称选择不当.JAVA不是课程列表.这是一门课程.因此应该命名枚举Course.


Nar*_*hai 12

如果要从枚举中传递单个值

    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);
    }
}
Run Code Online (Sandbox Code Playgroud)

否则,如果你想要通过整个班级,因为从问题中不清楚

private static void print(Class<?> generalInfo){

    }

    public static void main(String[] args) {
        print(GeneralInformation.class);
    }
Run Code Online (Sandbox Code Playgroud)

  • 或者如果你想要枚举类的所有实例,那么使用`GeneralInformation.values()`,它将为你提供包含所有枚举实例的GeneralInformation [] (4认同)
  • 哇,**这**有用: `cls.getEnumConstants()` 返回“一个数组,其中包含包含此 Class 对象所表示的枚举类的值(按声明顺序排列),如果此 Class 对象不表示枚举,则返回 null枚举类型”。测试了一下。奇迹般有效。 (2认同)

A. *_*czy 8

请注意,您的问题混淆了两个不同的问题:将枚举传递给函数或将枚举常量传递给函数.我的理解是你想要传递枚举本身,而不是它的一个常量函数.如果不是:请参阅Narendra Pathai关于如何将单个枚举常量传递给函数的答案.如果您不知道枚举和枚举常量之间的区别是什么,请查看有关枚举的文档 ...

我明白你想要的是有一个打印(或任何其他)函数,你可以传递任何可能的枚举,打印每个枚举的可能值(即常量).我发现以下两种方法可以做到这一点:

让我们说我们有以下枚举:

// The test enum, any other will work too
public static enum ETest
{
    PRINT,MY,VALUES
}
Run Code Online (Sandbox Code Playgroud)

Variante 1:将你的枚举中的常量数组传递给你的函数; 由于枚举的常量是静态值,因此可以轻松访问它们并将其传递给"print"函数,如下所示:

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));
}
Run Code Online (Sandbox Code Playgroud)

Variante 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()));
}
Run Code Online (Sandbox Code Playgroud)

在我看来,最好使用variante 1,因为variante 2中反射的过度使用.Viriante 2给你的唯一优点是你有Enum本身的Class对象(静态枚举,不仅是它的常量)在你的功能中,所以我已经提到了它的完整性.