enum是enumeration(列举)的简写形式,包含在java.lang包中。熟悉c, c++, c#, 或 pascal人应该对列举有所了解,先看个例子:
public enum season { winter, spring, summer, fall }
一个enum是定义一组值的对象,它可以包括零个或多个值成员。它是属于enum类型的,一个enum对象中不可有两个或多个相同的属性或值。在次之前的java程序员一般是 用接口的方法实现列举的,如 :
public interface season {
static winter = 0;
static spring = 1; //etc……
}
引入了enum的java的列举的编写方便了许多,只须定义一个enum型的对象。enum对象的值都回自动获得一个数字值,从0开始,依次递增。看一个比较简单的enum实现的例子:
enumdemo.java
package net.javagarage.enums;
/*
we can loop over the values we put into the enum
using the values() method.
note that the enum seasons is compiled into a
separate unit, called enumdemo$seasons.class
*/
public class enumdemo {
/*declare the enum and add values to it. note that, like in c#, we don't use a ; to
end this statement and we use commas to separate the values */
private enum seasons { winter, spring,
summer, fall }
//list the values
public static void main(string[] args) {
for (seasons s : seasons.values()){
system.out.println(s);
}
}
}运行上述代码你回得到 以下结果:
winter
spring
summer
fall
enum的属性调用:
下面的代码展示了调用enum对象的方法,这也是它通常的用法:
package net.javagarage.enums;
/*
file: enumswitch.java
purpose: show how to switch against the values in an enum.
*/
public class enumswitch {
private enum color { red, blue, green }
//list the values
public static void main(string[] args) {
//refer to the qualified value
doit(color.red);
}
/*note that you switch against the unqualified name. that is, "case color.red:" is a
compiler error */
private static void doit(color c){
switch (c) {
case red:
system.out.println("value is " + color.red);
break;
case green:
system.out.println("value is " + color.green);
break;
case blue:
system.out.println("value is : " + color.blue);
break;
default :
system.out.println("default");
}
}
为enums添加属性和方法
enums也可以象一般的类一样添加方法和属性,你可以为它添加静态和非静态的属性或方法,这一切都象你在一般的类中做的那样。
package net.javagarage.enums;
/*
file: enumdemo.java
purpose: show how to use an enum that also defines its own fields and methods
*/
public class enumwithmethods {
//declare the enum and add values to it.
public enum season {
winter, spring, summer, fall;
private final static string location = "phoenix";
public static season getbest(){
if (location.equals("phoenix"))
return winter;
else
return summer;
}
public static void main(string[] args) {
system.out.println(season.getbest());
}
}
就是这么的简单。但是有一点是需要注意的,那就是enums的值列表必须紧跟在enum声明,不然编译时将会出错。
enums构造函数:
和类一样enums也可以有自己的构造函数,如下:
package net.javagarage.enums;
public class enumconstructor {
public static void main(string[] a) {
//call our enum using the values method
for (temp t : temp.values())
system.out.println(t + " is : " + t.getvalue());
}
//make the enum
public enum temp {
absolutezero(-459), freezing(32),
boiling(212), paperburns(451);
//constructor here
temp(int value) {
this.value = value;
}
//regular field?but make it final,
//since that is the point, to make constants
private final int value;
//regular get method
public int getvalue() {
return value;
}
}
}输出结果是:
absolutezero is : -459
freezing is : 32
boiling is : 212
paperburns is : 451
尽管enums有这么多的属性,但并不是用的越多越好,如果那样还不如直接用类来的直接。enums的优势在定义int最终变量仅当这些值有一定特殊含义时。但是如果你需要的是一个类,就定义一个类,而不是enum.
闽公网安备 35060202000074号