1. 什么是枚举?枚举就是用来存放一组固定的常量。
2. 枚举有什么作用?一些程序在运行时,它需要的数据不能是任意的,而必须是一定范围内的值;例如性别 男和女。
public enum Gender{
男, 女;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Gender gender=Gender.男;
switch(gender){
case 男:
System.out.println("性别是男的");
break;
case 女:
System.out.println("性别是女的");
break;
default:
System.out.println("枚举没有这个值");
break;
}
}
3. 枚举的特性:一种特殊形式的java类;与java的普通类一样,在声明枚举类时,也可以声明属性 ,方法和构造函数。
4. 测试枚举的常用方法:name()获取枚举名称
ordinal() 获取枚举值在枚举中所处的位置
valueof(Class enumClass, String name) 讲字符串转换为枚举值
values()遍历枚举的所有枚举值
//测试枚举的常用方法
@Test
public void test2(){
System.out.println(Grade.A.name()); //获取枚举的名称
System.out.println(Grade.C.ordinal()); //枚举值在枚举中所处的位置
//将字符串转换成枚举值1111
String str="B";
Grade g= Grade.valueOf(Grade.class,str);
System.out.println("g------------"+g);
//将字符串装换成枚举值22222
Grade h=Grade.valueOf(str);
System.out.println("h-------------"+h);
//返回所有枚举值
Grade ga[]=Grade.values();
for(Grade gl:ga){
System.out.println("gl------------------"+gl);
}
}
下面贴出一个读书章节的代码:
package com.cn;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Random;
import org.junit.Test;
public class ClassTest {
int a=0;
int[] aa=new int[10];
static String lists=null;
String stuName="";
String stuValue="";
@Test
public String test(){
String names=null;
for(int i=0;i<2;i++){
System.out.println("test()-----------");
a=(int) (Math.random()*7);
for(int j=0;j<aa.length+1;j++){
aa[i]=a;
System.out.println(aa[i]);
}
System.out.println(aa.length);
String[] name=new String[]{"陈蓓","李春华","黄初欢","李永岩","刘承发","何纯洁","谢明雪"};
String[] list=new String[]{"第一章和第二章","第三章和第四章","第五章和第六章","第七章和第八章","第九章和第十章","第十一章和第十二章","第十三章和第十四章"};
/*Random random=new Random();
System.out.println("random--------------"+random.nextDouble());
int a=random.nextInt();
System.out.println("a----------------"+a);
String nem="FRI";*/
lists=list[(int) (Math.random()*7)];
System.out.println("list---------------"+lists);
names=name[a];
System.out.println("names--------------"+names);
//System.out.println("a--------"+a);
if(names.equals("李春华")){
print(Book.李春华);
}else if(names.equals("陈蓓")){
print(Book.陈蓓);
}else if(names.equals("黄初欢")){
print(Book.黄初欢);
}else if(names.equals("李永岩")){
print(Book.李永岩);
}else if(names.equals("刘承发")){
print(Book.刘承发);
}else if(names.equals("何纯洁")){
print(Book.何纯洁);
}else if(names.equals("谢明雪")){
print(Book.谢明雪);
}
}
return names;
}
public void print(Book k){
System.out.println("k-----------------"+k);
stuValue=k.localValue();
System.out.println("values--------------"+stuValue);
//return values;
}
public String classs(){
String value=stuValue;
System.out.println("valuesssssssssssss-------------"+value);
return value;
}
enum Book{
李春华("1")
{
public String localValue() {
// TODO Auto-generated method stub
return lists;
}
}
,黄初欢("2")
{
public String localValue() {
// TODO Auto-generated method stub
return lists;
}
}
,陈蓓("3")
{
public String localValue() {
// TODO Auto-generated method stub
return lists;
}
}
,李永岩("4")
{
public String localValue() {
// TODO Auto-generated method stub
return lists;
}
}
,何纯洁("5")
{ public String localValue() {
// TODO Auto-generated method stub
return lists;
}
}
,刘承发("6")
{
public String localValue() {
// TODO Auto-generated method stub
return lists;
}
}
,谢明雪("7"){
public String localValue() {
// TODO Auto-generated method stub
return lists;
}
} ;
private String value;
private Book(String value){
this.value=value;
}
private String getValue(){
return this.value;
}
//抽象方法
public abstract String localValue();
}
public static void main(String[] args){
/*ClassTest ct=new ClassTest();
ct.test();*/
}
}
index.jsp里面的代码:
<%@ page language="java" import="java.util.*,com.cn.ClassTest" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
ClassTest ct= new ClassTest();
String names=ct.test();
String values=ct.classs();
/*ct.test(String names);
String name=names;*/
//String value=values;
%>
<form method="post">
<label>中奖名单</label><input type="text" name="zhongjiang" value=<%=names %>>
<br/>
<<label>课程章节</label><input type="text" name="zhongjiang" value=<%=values %>>
<br/>
<input type="submit" value="抽奖"/>
</form>
</body>
</html>