How to pass enum as an argument in a method in java?

后端 未结 6 1133
走了就别回头了
走了就别回头了 2021-01-03 18:47
public class Enumvalues{

    enum courseList {
        JAVA,
        C,
        PYTHON,
        PERL
    }

    enum generalInformation {
        NAME,
        AGE,         


        
6条回答
  •  失恋的感觉
    2021-01-03 19:00

    If you want to pass a single value from the enum

        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);
        }
    }
    

    else if you want whole class to be passed then, as it was not clear from the question

    private static void print(Class generalInfo){
    
        }
    
        public static void main(String[] args) {
            print(GeneralInformation.class);
        }
    

提交回复
热议问题