枚舉,給枚舉賦值


/**************枚舉*****************/
// public enum Colors{
//  Red,Yellow,Blue,Black,White
// }
// public static void main(String[] args) {
//  Colors c = Colors.Yellow;
//  System.out.println(c);//輸出枚舉
//  System.out.println(c.ordinal());//輸出枚舉對應的序號(第一個枚舉元素是0,后面的依次+1)
//  
//  //將字符串轉換為枚舉
//  Colors c2 = Enum.valueOf(Colors.class, "Blue");
//  if(c2 == Colors.Blue){
//   System.out.println("轉換成功!");
//  }
//  //枚舉在switch中的使用
//  switch (c2) {
//   case Red:
//    System.out.println("紅色!");
//    break; 
//   case Yellow:
//    System.out.println("黃色!");
//    break; 
//   case Blue:
//    System.out.println("藍色!");
//    break; 
//   case Black:
//    System.out.println("黑色!");
//    break;
//   case White:
//    System.out.println("白色!");
//    break;
//   default:
//    break;
//  }
// }
 
 /**************給枚舉賦值***************/
// public enum Colors{
//  Red(1),Yellow(3),Blue(5);
//  private int _value;
//  Colors(int value){
//   _value=value;
//  }
//  int getValue(){
//   return _value;
//  }
// }
// public static void main(String[] args) {
//  for (Colors c : Colors.values()) {
//   System.out.println(c);//輸出枚舉元素
//   System.out.println(c.getValue());//輸出枚舉元素的值
//  }
// }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM