已有101人关注
第17章 枚举类型
发表在Java图书答疑 2014-04-24
是否精华
版块置顶:
《Java 从入门到精通》(第3版)第17章 例17.2
导入光盘中相对应的例题,用Eclipse运行。怎么显示错误?
在书写代码:enum Constants2 { // 将常量放置在枚举类型中
Constants_A, Constants_B
}
  时显示错误。

求解答!

源代码如下:

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);
// ConstantsTest.doit2(3);
}
}
分享到:
精彩评论 1
无语_mrkj
学分:3155 LV11
2014-05-27
沙发
请问具体是什么错误呢?
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照