enum 枚举类型

来源:互联网 发布:小米4怎么设置2g网络 编辑:程序博客网 时间:2024/06/02 13:42

实例介绍:

/**

 * @author
 * @description:
 * @tags:
 */
public class TestEnum {

------------1
/**
* 最普通的枚举
* @author
* @description:
* @tags:
*/
public enum ColorSelect{
//(红,绿,黄,蓝)
red, green, yellow, blue;
}

------------2
/**
* 枚举也可以像一般的类一样添加方法和属性,可以添加静态/非静态的属性或方法
* (一切都像和在一般类中做的那样)
* @author
* @description:
* @tags:
*/
public enum Season{
//(冬,春,夏,秋),必须写在最前面,否则编译会出错
winter, spring, summer, fall;

//添加属性
private final static String location = "Hot";

//添加方法
public static Season getBest(){
if(location.equals("Hot")){
return winter;
}else{
return summer;
}
}
}

------------3
/**
* 带有构造方法的枚举
* @author
* @description:
* @tags:
*/
public enum Temp{
//通过括号赋值,且必须有带参数的构造方法和属性,否则编译会出错
//(必须都赋值或都不赋值,如不赋值则不能写构造方法)
absoluteZero(-459), freezing(32), boiling(212), paperBurns(451);

private final int value;

public int getValue(){
return value;
}

//构造方法默认只能是private,保证只能内部使用
private Temp(int value){
this.value = value;
}
}

------------4

/**
* 带有构造方法的枚举
* @author
* @description:
* @tags:
*/
public enum MyContr{
//通过括号赋值,且必须有带参数的构造方法和属性,否则编译会出错
//(必须都赋值或都不赋值,如不赋值则不能写构造方法)
aab(-459,"aabNm"), aac(32,"aac"), aad(212,"aad");

private final int value;
private final String name;

public int getValue(){
return value;
}
public String getName(){
return name;
}

//构造方法默认只能是private,保证只能内部使用
private MyContr(int value, String name){
this.value = value;
this.name = name;
}
}


==============

==============

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


//枚举是一种类型,用于定义变量,赋值时通过“枚举名.值”来取得相关枚举中的值
System.out.println("out1---");
ColorSelect m = ColorSelect.blue;
switch(m){
case red:{System.out.println("color is red");break;}
case green:{System.out.println("color is green");break;}
case yellow:{System.out.println("color is yellow");break;}
case blue:{System.out.println("color is blue");break;}
}

System.out.println("\nout2---");
System.out.println("遍历 ColorSelect中的值");
for(ColorSelect c: ColorSelect.values()){
System.out.println(c);
}

//枚举值在变量中的索引位置
System.out.println("\nout3---");
System.out.println("枚举ColorSelect中的值有:"+ColorSelect.values().length+"个");
System.out.println(ColorSelect.red.name()+" "+ColorSelect.red.ordinal());
System.out.println(ColorSelect.green.name()+" "+ColorSelect.green.ordinal());
System.out.println(ColorSelect.yellow.name()+" "+ColorSelect.yellow.ordinal());
System.out.println(ColorSelect.blue.name()+" "+ColorSelect.blue.ordinal());

//枚举类型变量实现了比较接口(comparable)
System.out.println("\nout4---");
System.out.println(ColorSelect.red.compareTo(ColorSelect.green));

System.out.println("\nout5---");
System.out.println(ColorSelect.red.green.yellow.blue);

System.out.println("\nout6---");
System.out.println(Season.getBest());

System.out.println("\nout7---");
for(Temp t:Temp.values()){
System.out.println(t+"的值是"+t.getValue());
}
}

System.out.println("\nout8---");
for(MyContr t:MyContr.values()){
System.out.println(t+"的值是"+t.getValue()+" "+t.getName());

System.out.println(t.toString().equals("aab"));
}


}


//--------------print result-------------------

out1---
color is blue

out2---
遍历 ColorSelect中的值
red
green
yellow
blue

out3---
枚举ColorSelect中的值有:4个
red 0
green 1
yellow 2
blue 3

out4---
-1

out5---
blue

out6---
winter

out7---
absoluteZero的值是-459
freezing的值是32
boiling的值是212
paperBurns的值是451


out8---
aab的值是-459 aabNm
true
aac的值是32 aac
false
aad的值是212 aad
false


原创粉丝点击