当前位置:编程学习 > JAVA >>

java enum的用法

public enum Type {
   IN("输入参数"),
   OUT("输出参数"),
   VAR("变量");
 
 
 
  //定义有参数的构造方法,这样就可以这样写了:IN("....")
   private String des;
   Type(String des)
   {
    this.des=des;
   }
   public String getDesc()
   {
    return des;
   }
}
 
 
 
 
  public enum State {   
        ON{
 
   @Override
   String getInfo() {
     return "开";
   }
        }, OFF  {
 
   @Override
   String getInfo() {
    // TODO Auto-generated method stub
    return null;
   }
         
        };
        //让每个值扩展一个抽象方法
        abstract String getInfo();
    };   
 
 
enum的java原型:
public final class Type extends Enum
{
 
 public static final Type IN;
 public static final Type OUT;
 public static final Type VAR;
 private String des;
 private static final Type ENUM$VALUES[];
 
 private Type(String s, int i, String des)
 {
  super(s, i);
  this.des = des;
 }
 
 public String getDesc()
 {
  return des;
 }
 
 public static Type[] values()
 {
  Type atype[];
  int i;
  Type atype1[];
  System.arraycopy(atype = ENUM$VALUES, 0, atype1 = new Type[i = atype.length], 0, i);
  return atype1;
 }
 
 public static Type valueOf(String s)
 {
  return (Type)Enum.valueOf(com/sunline/flow/ide/project/navigator/model/Type, s);
 }
 
 static
 {
  IN = new Type("IN", 0, "输入参数");
  OUT = new Type("OUT", 1, "输出参数");
  VAR = new Type("VAR", 2, "变量");
  ENUM$VALUES = (new Type[] {
   IN, OUT, VAR
  });
 }
}

 

作者“lizhensan”
 

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,