• 枚举类型—enum关键字


     1 public class Enum {
     2     public enum Spiciness {
     3         YOU, ME, HE, SHE, HER, YOUS
     4     }
     5     public Enum(Spiciness degree){
     6         this.degree=degree;
     7     }
     8     
     9     Spiciness degree;
    10 
    11     public void describe() {
    12         switch (degree) {
    13         case YOU:System.out.println("Are you");break;
    14         case ME:System.out.println("am I");break;
    15         case HE:System.out.println("is he");break;
    16         case SHE:System.out.println("is she");break;
    17         case HER:System.out.println("is her");break;
    18         case YOUS:System.out.println("Are yous");break;
    19         default:System.out.println("all no !");break;
    20         }
    21     }
    22 
    23     /**
    24      * 枚举
    25      */
    26     public static void main(String[] args) {
    27         
    28 
    29         // Spiciness[] s=Spiciness.values();
    30         // for(Spiciness x:Spiciness.values()){
    31         // System.out.print(x+" ");
    32         // }
    33         
    34         Enum one=new Enum(Spiciness.ME),two=new Enum(Spiciness.YOU),three=new Enum(Spiciness.HE);
    35         one.describe();
    36         two.describe();
    37         three.describe();
    38 
    39     }
    40 
    41 }

      enum看起来像是一种新的数据类型,事实上,enum确实是类,并且具有自己的方法。
      enum有一个特别实用的特性,即它可以在switch语句中使用,由于switch是要在有限的可能值集合中进行选择,因此它与enum正是绝佳的组合。

      请注意enum的名字是如何能够倍加清楚地表明程序的意欲何为的。

  • 相关阅读:
    String,StringBuffer与StringBuilder的区别?
    Digui
    Digui1
    逆序
    TestOverWrite
    DemoBoxWeight
    TestSuperSub
    Cast
    TestOverWrite
    Joseph
  • 原文地址:https://www.cnblogs.com/huanglibin/p/2754462.html
Copyright © 2020-2023  润新知