绪论
枚举类型可以取代以往的常量定义方式,即将常量封装在类或接口中。
枚举类型实质上还是以类的形式出现,泛型的出现可以让程序员少写代码,主要作用时解决类型安全问题。
枚举类型
枚举类型取代长量的定义方法,同时还赋予程序在编译时进行检查的功能
案例:使用枚举体定义常量
package 枚举类型与泛型;
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;
}
//定义一个方法,参数为int型
public static void doit1(int c) {
switch (c) {
case Constants.Constants_A :
System.out.println("doit() Constants_A");
break;
case Constants.Constants_B :
System.out.println("doit1()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.doit1(Constants.Constants_A);
//使用枚举体中定义的常量
ConstantsTest.doit2(Constants2.Constants_A);
ConstantsTest.doit2(Constants2.Constants_B);
}
}
操作枚举类型成员的方法
案例一:使用枚举类型方法获取成员变量
package 枚举类型与泛型;
import javax.swing.plaf.PanelUI;
public class ShowEnum {
enum Constants2 {
Constants_A,
Constants_B
}
public static void main(String args[]) {
//循环由values()方法返回的数组
for (int i = 0 ; i < Constants2.values().length ; i++) {
//将枚举成员变量打印
System.out.println(Constants2.values()[i]);
}
}
}
案例二:
package 枚举类型与泛型;
public class EnumMethodTest {
enum Constants2 {
//将常量放置在枚举类型中
Constants_A,
Constants_B
}
//定义比较枚举类型的方法,参数为枚举类型
public static void compare (Constants2 c) {
//根据values()方法返回数组做循环操作
for (int i = 0 ; i < Constants2.values().length; i++) {
System.out.println(c + "与" + Constants2.values()[i] + "比较结果是" + c.compareTo(Constants2.values()[i]));
}
}
//在主方法中调用比较方法
public static void main(String args[]) {
//以下俩种写法等价
EnumMethodTest.compare(Constants2.Constants_B);
EnumMethodTest.compare(Constants2.valueOf("Constants_B"));
//比较结果,正值代表方法中参数在调用该方法的枚举对象之前
}
}
案例三:
package 枚举类型与泛型;
public class EnumIndexTest {
enum Constants2 {
Constants_A,
Constants_B,
Constants_C
}
public static void main(String args[]) {
for (int i=0; i<Constants2.values().length; i++) {
System.out.println(Constants2.values()[i] + "在枚举类型中的位置为:" + Constants2.values()[i].ordinal());
}
}
}
枚举类型中的构造方法
//枚举类型中无论是无参构造方法还是有参数构造方法,都是用权限修饰符private
package 枚举类型与泛型;
public class EnumIndexTest_01 {
enum Constants2 {
//定义有参数的枚举类型成员
Constants_A("我是枚举成员A"),
Constants_B("我是枚举成员B"),
Constants_C("我是枚举成员C"),
Constants_D(3);
private String description;
private int i = 4;
private Constants2() {
}
//定义参数为description的构造方法
private Constants2(String description) {
this.description = description;
}
//定义参数为i的构造方法
private Constants2(int i) {
this.i = this.i + i;
}
//获取i的值
public int getI() {
return i;
}
//获取description的值
public String getDescription() {
return description;
}
}
public static void main(String args[]) {
for (int i = 0 ; i < Constants2.values().length; i++) {
System.out.println(Constants2.values()[i] + "调用getDescription()方法为:" + Constants2.values()[i].getDescription());
}
System.out.println("Constants_D调用getI()方法为:" + Constants2.valueOf("Constants_D").getI() );
}
}
个人小结:
定义有参数构造方法后,需要对成员变量使用该方法,根据成员变量初始化类型,再相应的构造返回值为该类型的构造方法,在该构造方法中为成员变量赋值,这样即可以在枚举类型中定义成员变量的getXXX()方法。
接口实现枚举类型
//将getXXX()方法放置在接口中,使枚举类型实现该接口,然后使每个枚举类型实现接口中的方法
package 枚举类型与泛型;
interface d {
public String getDescription();
public int getI();
}
public enum AnyEnum implements d {
//在枚举类型内部设置构造方法
Constants_A{
public String getDescription() {
return ("我是枚举类型A");
}
public int getI() {
return i;
}
},
Constants_B{
public String getDescription() {
return ("我是枚举类型B");
}
public int getI() {
return i;
}
},
Constants_C{
public String getDescription() {
return ("我是枚举类型C");
}
public int getI() {
return i;
}
},
Constants_D{
public String getDescription() {
return ("我是枚举类型D");
}
public int getI() {
return i;
}
};
private static int i = 5;
public static void main(String args[]) {
for (int i = 0; i < AnyEnum.values().length; i++) {
System.out.println(AnyEnum.values()[i] + "调用getDescription()方法为:" + AnyEnum.values()[i].getDescription());
System.out.println(AnyEnum.values()[i] + "调用getI()方法为:" + AnyEnum.values()[i].getI());
}
}
}
个人小结
枚举类型具有类型安全、紧凑有效的数据定义、可以与其它程序其它部分完美交互、运行效率高等特点。