标题:Java中自定义枚举类,及枚举类的测试
一、自定义枚举类
1)第一种自定义类来实现枚举类
//第一种自定义类来实现枚举类
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();
public static final Grade E=new Grade();
}
2)第二种自定义类来实现枚举类
//第二种自定义类来实现枚举类
class Grade02{
private String name;
private Grade02(String name) {
this.name=name;
}
public static final Grade02 A=new Grade02("hhhA");
public static final Grade02 B=new Grade02("hhhB");
public static final Grade02 C=new Grade02("hhhC");
public String getName() {
return name;
}
}
3)第三种自定义类来实现枚举类
//第三种自定义类来实现枚举类
abstract class Grade03{
private String name;
private Grade03(String name) {
this.name=name;
}
protected abstract void show();
public String getName() {
return this.name;
}
public static final Grade03 A=new Grade03("hhhA") {
protected void show() {
System.out.println("jjjA");
}
};
public static final Grade03 B=new Grade03("hhhB") {
protected void show() {
System.out.println("jjjB");
}
};
}
二、使用枚举类
1)方式一
//方式一
enum Gra{
A,B,C;
}
2)方式二
//方式二
enum Gra02{
A("A"),B("B"),C("C");
private String name;
private Gra02(String name) {
this.name=name;
}
public String getName() {
return this.name;
}
}
3)方式三
//方式三
enum Gra03{
A("A"){
public void show() {
System.out.println(this.getName());
}
},
B("B"){
public void show() {
System.out.println(this.getName());
}
},
C("C"){
public void show() {
System.out.println(this.getName());
}
};
private String name;
private Gra03(String name) {
this.name=name;
}
public String getName() {
return this.name;
}
public abstract void show();
}
三、完整代码如下:
/**
* 测试枚举类
* @author dell
*
*/
public class TestEnum {
//jdk5之前
public void print(Grade02 g) {//只想要输入A、B、C、D、E
System.out.println(g.getName());
}
//测试第二种自定义枚举类
@Test
public void test() {
this.print(Grade02.B);
}
//测试第三种自定义枚举类
@Test
public void test02() {
Grade03.A.show();
}
//测试枚举类
//第一种
@Test
public void tes() {
System.out.println(Gra.A);
}
//第二种
@Test
public void tes02() {
System.out.println(Gra02.A.getName());
}
//第三种
@Test
public void tes03() {
Gra03.A.show();
}
}
//第一种自定义类来实现枚举类
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();
public static final Grade E=new Grade();
}
//第二种自定义类来实现枚举类
class Grade02{
private String name;
private Grade02(String name) {
this.name=name;
}
public static final Grade02 A=new Grade02("hhhA");
public static final Grade02 B=new Grade02("hhhB");
public static final Grade02 C=new Grade02("hhhC");
public String getName() {
return name;
}
}
//第三种自定义类来实现枚举类
abstract class Grade03{
private String name;
private Grade03(String name) {
this.name=name;
}
protected abstract void show();
public String getName() {
return this.name;
}
public static final Grade03 A=new Grade03("hhhA") {
protected void show() {
System.out.println("jjjA");
}
};
public static final Grade03 B=new Grade03("hhhB") {
protected void show() {
System.out.println("jjjB");
}
};
}
/
//枚举类
//方式一
enum Gra{
A,B,C;
}
//方式二
enum Gra02{
A("A"),B("B"),C("C");
private String name;
private Gra02(String name) {
this.name=name;
}
public String getName() {
return this.name;
}
}
//方式三
enum Gra03{
A("A"){
public void show() {
System.out.println(this.getName());
}
},
B("B"){
public void show() {
System.out.println(this.getName());
}
},
C("C"){
public void show() {
System.out.println(this.getName());
}
};
private String name;
private Gra03(String name) {
this.name=name;
}
public String getName() {
return this.name;
}
public abstract void show();
}