如何使用枚举类型,在JAVA中? 听说JDK5.0中添加了枚举类型,谁能举个简单的例子,使用枚举?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 就跟你以前用C结构体一样啊,Week.Monday...... 在JDK5.0中新增了枚举类型,所有的枚举类型都是enum的子类.简单定义如下:public enum Size{SMALL,MEDIUM,LARGE};引用方式Size.SMALL具体你可以看 corejava第七版里的介绍.或者JDK5.0里的帮助.Enum 用法:public enum Name {types, ….}简单的例子:public enum Colors {Red, Yellow, Blue, Orange, Green, Purple, Brown, Black}public static void main(String[] args){ Colors myColor = Colors.Red; System.out.println(myColor);}又一个简单例子:import java.util.*;enum OperatingSystems {windows, unix, linux, macintosh}public class EnumExample1 { public static void main(String args[]) { OperatingSystems os; os = OperatingSystems.windows; switch(os) { case windows: System.out.println(“You chose Windows!”); break; case unix: System.out.println(“You chose Unix!”); break; case linux: System.out.println(“You chose Linux!”); break; case macintosh: System.out.println(“You chose Macintosh!”); break; default: System.out.println(“I don’t know your OS.”); break; } }}应运enum简写的例子:import java.util.*; public class EnumTest{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) "); String input = in.next().toUpperCase(); Size size = Enum.valueOf(Size.class, input); System.out.println("size=" + size); System.out.println("abbreviation=" + size.getAbbreviation()); if (size == Size.EXTRA_LARGE) System.out.println("Good job--you paid attention to the _."); }} enum Size{ SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL"); private Size(String abbreviation) { this.abbreviation = abbreviation; } public String getAbbreviation() { return abbreviation; } private String abbreviation;}enum类中拥有方法的一个例子:enum ProgramFlags { showErrors(0x01), includeFileOutput(0x02), useAlternateProcessor(0x04); private int bit; ProgramFlags(int bitNumber) { bit = bitNumber; } public int getBitNumber() { return(bit); }}public class EnumBitmapExample { public static void main(String args[]) { ProgramFlags flag = ProgramFlags.showErrors; System.out.println(“Flag selected is: “ + flag.ordinal() + “ which is “ + flag.name()); }}来自于google 还是拿这个例子问大家!import java.util.*;enum OperatingSystems {windows, unix, linux, macintosh}public class EnumExample1 { public static void main(String args[]) { OperatingSystems os; os = OperatingSystems.windows; switch(os) { case windows: //这里的windows是枚举类OperatingSystems 里面的一个成员变量,为什么可以拿出来,可且能够被识别? System.out.println(“You chose Windows!”); break; default: System.out.println(“I don’t know your OS.”); break; } }}而且,Enum跟enum有什么关系? 怎么读写网络上的XML文件? 求助:SCJP考题一道 - 有关HashSet的remove方法与元素赋值 java的时区问题,为何转换不过来? netbeans及mysql数据库的高手请进 一个hibernate dynamic-update配置的问题 关于用this传递对象的问题 java用命令编译 请各位进来看一下,以我现在对java了解成度,评介一下我说的对吧? 调用Appletviewer后,MyApplet为何显示没内容 GUI界面设计,大小 不能精确掌握!求助!! DocumentListener.changedUpdate() 在何时被触发 请各位达人们答疑解惑
public enum Size{SMALL,MEDIUM,LARGE};
引用方式Size.SMALL
具体你可以看 corejava第七版里的介绍.或者JDK5.0里的帮助.Enum
来自于google
import java.util.*;
enum OperatingSystems {windows, unix, linux, macintosh}
public class EnumExample1 {
public static void main(String args[]) {
OperatingSystems os;
os = OperatingSystems.windows;
switch(os) {
case windows: //这里的windows是枚举类OperatingSystems 里面的一个成员变量,为什么可以拿出来,可且能够被识别?
System.out.println(“You chose Windows!”);
break;
default:
System.out.println(“I don’t know your OS.”);
break;
}
}
}而且,Enum跟enum有什么关系?