java枚举与及其switch的使用

一些程序在运行时,它需要的数据不能是任意的 而必须是一定范围内的值,jdk5以前常采用自定义类来解决

package com.xxw.enumeration;

import org.junit.Test;

/*
 * 一些程序在运行时,它需要的数据不能是任意的
 * 而必须是一定范围内的值,jdk5以前常采用自定义类
 * 来解决,jdk 5以后可以直接采用枚举的解决。
 */

//自定义类
/*
 *以学生成绩为例子 
 *
 */

public class Demo01 {
	
    @Test
	public static void test(){
		print(Grade.A);
	}
	public static void print(Grade g){
		System.out.println(g.getValue());
	}

}
class Grade{
	private String value;
	private Grade(){//构造函数
		
	}
	private Grade(String value){
		this.value=value;
	}
	public String getValue(){//设置数据取出端口
		return this.value;
	}
	public static final Grade A = new Grade("100-90");//自定义枚举
	public static final Grade B = new Grade();
	public static final Grade C = new Grade();
	public static final Grade D = new Grade();
	public static final Grade E = new Grade();
}

运行结果:


由上边的代码中我们很容易就可以看到,jdk1.5以前我们要用一个牵制数据的过程是比较复杂的,在jdk1.5以后,我们可以使用枚举类型

</pre><pre name="code" class="java">package com.xxw.enumeration02;

import org.junit.Test;

/*
 * 枚举中可以拥有抽象方法和构造函数,
 * 
 * 
 * 
 * 
 */
enum Grade{
	//相当于public static final Grade A = new Grade("100-90"){匿名内部类:如果只是用该类只实现一次,使用匿名内部类}
     A("100-90"){
    	 //实例化必须实现抽象方法
    	 public String setGrade(){//实现抽象类
            return "优";
    	 }
     },
     B("89-80"){
    	 public String setGrade(){
             return "优";
     	 }
     },
     C("79-70"){
    	 public String setGrade(){
             return "优";
     	 }
     },
     D("69-60"){
    	 public String setGrade(){
             return "优";
     	 }
     },
     E("59-0"){
    	 public String setGrade(){
             return "优";
     	 }
     };
	
	/**
	 *@param value Receive the customer to get the information 接收客户传过来的信息
	 */
	private String value;
	//构造函数
    private Grade(){
    	
    }
    //有参构造函数
    private Grade(String value){
    	this.value = value;
    }
    //设置一个值来取出value
    public String getValue(){
    	return this.value;
    }
    //抽象方法
    public abstract String setGrade();
}

public class Demo02 {
	  @Test
      public void test(){
    	 print(Grade.A);
      }
      public void print(Grade g){
    	  String value = g.setGrade();
    	  System.out.println(value);
    	  //测试枚举类型的常用方法
    	  
    	  System.out.println(g.name());//取得当前枚举名
    	  int i = g.ordinal();//取得当前值在枚举类中位置
    	  System.out.println(i);
    	  //检测枚举类中是否有该枚举
    	  /*str--->枚举
    	   * 在验证表单提交过来的信息中有重要的作用
    	   *    如验证男女  没有则报错
    	   */
    	  String str = "C";
    	  Grade detection = g.valueOf(str);
    	  System.out.println(detection);
    	  
    	 /* String str02 = "F";
    	  Grade detection02 = g.valueOf(str02);
    	  System.out.println(detection02);
    	  */
    	  //拿到所有值
    	  System.out.println("===================");
         Grade gs[] = g.values();
         for(int j = 0; j<gs.length;j++ ){
        	 System.out.println(gs[j]);
        	 System.out.println();
         }
    	  
    	  
      }

      
      
}
运行结果:

A
0
C
===================
A


B


C


D


E

一般类型的枚举,通常是不能够满足的我们的需求的,比如在我们在输入成绩后,要知道一个等级的判断(上例中使用优,良等进行拆分),简单的枚举显然不能够满足我们的需求,于是我们会使用抽象方法进行补充,但是必须注意的是:A的实例必须同时实现抽象方法,我们采用匿名内来进行处理

//相当于public static final Grade A = new Grade("100-90"){匿名内部类:如果只是用该类只实现一次,使用匿名内部类}
     A("100-90"){
    	 //实例化必须实现抽象方法
    	 public String setGrade(){//实现抽象类
            return "优";
    	 }
     }
<strong><span style="font-size:18px;">siwtch用法</span></strong>
<span style="font-size:14px;">首先我们先创建一个枚举类</span>
<span style="font-size:14px;">package com.xxw.enumeration03;


enum WeekDay{
<span style="white-space:pre">	</span>MON,TUS,WEN,THU,FRI,SAT,SUN;


}
接下来是switch的应用</span>
<span style="font-size:14px;">package com.xxw.enumeration03;


import org.junit.Test;


class TestSwitch{
<span style="white-space:pre">	</span>private WeekDay weekday;
<span style="white-space:pre">	</span>public TestSwitch(WeekDay weekday){
<span style="white-space:pre">		</span>   this.weekday = weekday;
<span style="white-space:pre">	</span>   }
<span style="white-space:pre">	</span>public void display(){
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>   switch(weekday){
<span style="white-space:pre">	</span>   case SUN:
<span style="white-space:pre">		</span>   System.out.println("星期日");
<span style="white-space:pre">		</span>   break;
<span style="white-space:pre">	</span>   case SAT:
<span style="white-space:pre">		</span>   System.out.println("星期六");
<span style="white-space:pre">		</span>   break;
<span style="white-space:pre">	</span>   case FRI:
<span style="white-space:pre">		</span>   System.out.println("星期五");
<span style="white-space:pre">		</span>   break;
<span style="white-space:pre">	</span>   case THU:
<span style="white-space:pre">		</span>   System.out.println("星期四");
<span style="white-space:pre">		</span>   break;
<span style="white-space:pre">	</span>   case WEN:
<span style="white-space:pre">		</span>   System.out.println("星期三");
<span style="white-space:pre">		</span>   break;
<span style="white-space:pre">	</span>   case TUS:
<span style="white-space:pre">		</span>   System.out.println("星期二");
<span style="white-space:pre">		</span>   break;
<span style="white-space:pre">	</span>   case MON:
<span style="white-space:pre">		</span>   System.out.println("星期一");
<span style="white-space:pre">		</span>   break;
<span style="white-space:pre">	</span>   default:
<span style="white-space:pre">	</span>           System.out.println("您输入有误");
<span style="white-space:pre">	</span>     }
<span style="white-space:pre">	</span>   }
<span style="white-space:pre">	</span> }




public class Demo03 {
   @Test
   public void test(){
<span style="white-space:pre">	</span>   TestSwitch ts = new TestSwitch(WeekDay.SUN);
       ts.display(); 
     }
}
</span>
<span style="font-size:14px;">运行结果</span>
<span style="font-size:14px;">     星期日</span>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值