枚举类型案列1

package 枚举类型与泛型;

public class ConstantsTest {
    enum Constants2 {// 将常量放置在枚举类型中
        Constants_A, Constants_B
    }

    // 使用接口定义常量
    public static void doit(int c) {// 定义一个参数为int型的方法
        switch (c) {// 根据常常量的不同作不同的操作
        case Constants.Constants_A:
            System.out.println("doit() Constants_A");
            break;
        case Constants.COnstants_B:
            System.out.println("doit() Constants_b");
            break;
        }
    }

    public static void doit2(Constants2 c) {
        switch (c) {
        case Constants_A:
            System.out.println("doit2() Constants_A");
            break;
        case Constants_B:
            System.out.println("doit2() Constants_B");
            break;
        }
    }
    public static void main(String...strings) {
        ConstantsTest.doit(Constants.Constants_A);//使用接口中定义的常量
        ConstantsTest.doit2(Constants2.Constants_A);//使用枚举类型中的常量
        ConstantsTest.doit2(Constants2.Constants_B);//使用枚举类型中的常量
        ConstantsTest.doit(3);
        //ConstantsTest.doit2(3);
    }
}

运行结果:

doit() Constants_A
doit2() Constants_A
doit2() Constants_B


猜你喜欢

转载自blog.csdn.net/qq_41978199/article/details/80696158
今日推荐