本帖最后由 pzfpang449 于 2013-1-5 17:50 编辑
5,枚举
一些程序在运行时,它需要的数据不能是任意的,而必须是一定范围内的值,jdk5以前采用的自定义类来解决,jdk5以后可以直接采用枚举解决
package com.itcast.enumeration;
import org.junit.Test;
public class meiju {
//限定只可以传A B C D E
@Test
public void test(){
print(Grade.B);
}
public void print(Grade g){//限定只可以传A B C D E
String value= g.getValue();
System.out.println(value);
}
}
//传统方法,自定义
/*class Grade{
private Grade(){};
public static final Grade A=new Grade();
public static final Grade B=new Grade();
public static final Grade C=new Grade();
public static final Grade D=new Grade();
}*/
//如何定义枚举的构造函数、方法个字段,去封装更多的信息
enum Grade{
A("100-90"),B("89-80"),C("79-70"),D("69-60"),E("59-50");
private String value;
private Grade(String value){
this.value=value;
}
public String getValue(){
return this.value;
}
}
输出结果为89-80
//带抽象方法的枚举
package com.itcast.enumeration;
import org.junit.Test;
public class CopyOfmeiju {
//限定只可以传A B C D E
@Test
public void test(){
print(Grade1.B);
}
@Test
public void test2(){
//获取枚举的名称
System.out.println(Grade.C.name());
//获取枚举的位置
System.out.println(Grade.C.ordinal());
String str="B";
Grade1 g=Grade1.valueOf(str);
System.out.println(g);
//遍历枚举的所有值
Grade gs[]= Grade.values();
for(Grade g2:gs){
System.out.println(g2);
}
}
public void print(Grade1 g){//限定只可以传A B C D E
String value= g.localValue();
System.out.println(value);
}
}
//传统方法,自定义
/*class Grade{
private Grade(){};
public static final Grade A=new Grade();
public static final Grade B=new Grade();
public static final Grade C=new Grade();
public static final Grade D=new Grade();
}*/
//如何定义枚举的构造函数、方法个字段,去封装更多的信息
enum Grade1{
A("100-90"){
public String localValue(){
return "优秀";
}
}
,B("89-80"){
public String localValue(){
return "良好";
}
}
,C("79-70"){
public String localValue(){
return "一般";
}
}
,D("69-60"){
public String localValue(){
return "及格";
}
}
,E("59-50"){
public String localValue(){
return "不及格";
}
};
private String value;
private Grade1(String value){
this.value=value;
}
public String getValue(){
return this.value;
}
public abstract String localValue();
}
输出结果为 良好
枚举具有的特点:
1,枚举是一种特殊形式的java类
2,枚举中声明的每一个枚举值代表枚举类的一个实例对象
3,枚举类的构造函数必须为私有的
4,枚举也可以实现接口、或继承抽象类
5,JDK5中扩展了switch语句,它除了可以接受int,byte,char,short外,还可以接受一个枚举类型
6,若枚举类只有一个枚举值,则可以当做单态设计模式使用
枚举例子,中文星期*
package com.itcast.enumeration;
import org.junit.Test;
public class WeekDAY1 {
public void prin(WeekDay day){
String chiDay=day.chinaWeek();
System.out.println(chiDay);
}
@Test
public void Test(){
prin(WeekDay.MON);
}
}
enum WeekDay{
MON("1"){
public String chinaWeek(){
return "星期一";
}
}
,TUE("2"){
public String chinaWeek(){
return "星期二";
}
}
,WED("3"){
public String chinaWeek(){
return "星期三";
}
}
,THU("4"){
public String chinaWeek(){
return "星期四";
}
}
,FRI("5"){
public String chinaWeek(){
return "星期五";
}
}
,SAT("6"){
public String chinaWeek(){
return "星期六";
}
}
,SUN("7"){
public String chinaWeek(){
return "星期七";
}
};
private String day;
private WeekDay(String day)
{
this.day=day;
}
public String getDay(String day){
return this.day;
}
public abstract String chinaWeek();
}
结果为星期一
|