A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ATT丨bestcjy 中级黑马   /  2015-12-4 23:11  /  558 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

interface Constants {                                                                         // 将常量放置在接口中
        public static final int Constants_A = 1;
        public static final int Constants_B = 12;
}

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[] args) {
                ConstantsTest.doit(Constants.Constants_A);             // 使用接口中定义的常量
                ConstantsTest.doit2(Constants2.Constants_A);         // 使用枚举类型中的常量
                ConstantsTest.doit2(Constants2.Constants_B);         // 使用枚举类型中的常量
                ConstantsTest.doit(3);
       
        }
}

评分

参与人数 1黑马币 +3 收起 理由
张研老师 + 3 神马都是浮云

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马