Java_枚舉類。本站提示廣大學習愛好者:(Java_枚舉類)文章只能為提供參考,不一定能成為您想要的結果。以下是Java_枚舉類正文
枚舉類
枚舉類不出來的時分只能如下寫代碼
1 //Student.java 2 package cn.itcast.enumeration; 3 4 public class Student { 5 private String name; 6 private String grade;//A B C D E 7 public String getName() { 8 return name; 9 } 10 public void setName(String name) { 11 this.name = name; 12 } 13 public String getGrade() { 14 return grade; 15 } 16 public void setGrade(String grade) { 17 if(!grade.matches("[ABCDE]")){ 18 throw new RuntimeException("成果輸出錯誤"); 19 } 20 this.grade = grade; 21 } 22 }
第17行代碼運用了正則表達式。
1 //Test.java 2 package cn.itcast.enumeration; 3 4 public class Test { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 Student s = new Student(); 9 s.setGrade("H"); 10 } 11 }
這個代碼有一點不好,就是給賦值了,但是順序運轉是才報錯。如何讓寫代碼的時分就報錯?看如下代碼。
1 //Student.java 2 package cn.itcast.enumeration2; 3 4 public class Student { 5 private String name; 6 private Grade grade;//A B C D E 7 public String getName() { 8 return name; 9 } 10 public void setName(String name) { 11 this.name = name; 12 } 13 public Grade getGrade() { 14 return grade; 15 } 16 public void setGrade(Grade grade) { 17 this.grade = grade; 18 } 19 } 20 21 class Grade{ 22 private Grade(){} 23 24 public static Grade A = new Grade(); 25 public static Grade B = new Grade(); 26 public static Grade C = new Grade(); 27 public static Grade D = new Grade(); 28 public static Grade E = new Grade(); 29 }
定義一個自己運用的類,再賦值的時分只能賦指定的值,其他值都報錯。
1 //Test.java 2 package cn.itcast.enumeration2; 3 4 public class Test { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 Student s = new Student(); 9 s.setGrade(Grade.A); 10 } 11 }
這種辦法太復雜,上面運用枚舉,只需三行就能取代21-29行的代碼
1 //Student.java 2 package cn.itcast.enumeration3; 3 4 public class Student { 5 private String name; 6 private Grade grade;//A B C D E 7 public String getName() { 8 return name; 9 } 10 public void setName(String name) { 11 this.name = name; 12 } 13 public Grade getGrade() { 14 return grade; 15 } 16 public void setGrade(Grade grade) { 17 this.grade = grade; 18 } 19 } 20 /* 21 class Grade{ 22 private Grade(){} 23 24 public static Grade A = new Grade(); 25 public static Grade B = new Grade(); 26 public static Grade C = new Grade(); 27 public static Grade D = new Grade(); 28 public static Grade E = new Grade(); 29 } 30 */ 31 enum Grade{//也是定義了一個類 32 A,B,C,D,E;//對應的是Grade類的對象 33 }
運用枚舉類取代21-29行的代碼。
1 package cn.itcast.enumeration3; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Student s = new Student(); 8 s.setGrade(Grade.A); 9 } 10 }
這種辦法比擬好,但是如何在枚舉類外面封裝更多的信息?看上面的代碼
1 //Student.java 2 package cn.itcast.enumeration4; 3 4 public class Student { 5 private String name; 6 private Grade grade;//A B C D E 7 public String getName() { 8 return name; 9 } 10 public void setName(String name) { 11 this.name = name; 12 } 13 public Grade getGrade() { 14 return grade; 15 } 16 public void setGrade(Grade grade) { 17 this.grade = grade; 18 } 19 } 20 /* 21 class Grade{ 22 private Grade(){} 23 24 public static Grade A = new Grade(); 25 public static Grade B = new Grade(); 26 public static Grade C = new Grade(); 27 public static Grade D = new Grade(); 28 public static Grade E = new Grade(); 29 } 30 */ 31 //如何讓枚舉封裝更多的信息(如何讓枚舉對象擁有自己的屬性) 32 enum Grade{//也是定義了一個類 33 A("100-90"),B("89-80"),C("79-70"),D("69-60"),E("59-0");//對應的是Grade類的對象 34 35 private String value; 36 private Grade(String value){ 37 this.value = value; 38 } 39 public String getValue(){ 40 return value; 41 } 42 }
封裝成果的同時,可以封裝分數段,添加屬性。
1 //Test.java 2 package cn.itcast.enumeration4; 3 4 public class Test { 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Student s = new Student(); 8 s.setGrade(Grade.A); 9 System.out.println(Grade.A.getValue()); 10 } 11 }
那麼如何在枚舉類外面封裝各自的辦法呢?看上面的代碼。
1 //Student.java 2 package cn.itcast.enumeration6; 3 4 public class Student { 5 private String name; 6 private Grade grade;//A B C D E 7 public String getName() { 8 return name; 9 } 10 public void setName(String name) { 11 this.name = name; 12 } 13 public Grade getGrade() { 14 return grade; 15 } 16 public void setGrade(Grade grade) { 17 this.grade = grade; 18 } 19 } 20 /* 21 class Grade{ 22 private Grade(){} 23 24 public static Grade A = new Grade(); 25 public static Grade B = new Grade(); 26 public static Grade C = new Grade(); 27 public static Grade D = new Grade(); 28 public static Grade E = new Grade(); 29 } 30 */ 31 //如何讓枚舉對象擁有自己的屬性和各自的辦法 32 enum Grade{//也是定義了一個類 33 A("100-90"){ 34 public String toLocalString(){ 35 return "優"; 36 } 37 },B("89-80"){ 38 public String toLocalString(){ 39 return "良"; 40 } 41 },C("79-70"){ 42 public String toLocalString(){ 43 return "中"; 44 } 45 },D("69-60"){ 46 public String toLocalString(){ 47 return "普通"; 48 } 49 },E("59-0"){ 50 public String toLocalString(){ 51 return "差"; 52 } 53 };//對應的是Grade類的對象 54 55 private String value; 56 private Grade(String value){ 57 this.value = value; 58 } 59 public String getValue(){ 60 return value; 61 } 62 public abstract String toLocalString(); 63 }
前往成果的中文表示。
1 package cn.itcast.enumeration6; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Student s = new Student(); 8 s.setGrade(Grade.A); 9 System.out.println(Grade.A.getValue()); 10 System.out.println(Grade.A.toLocalString()); 11 } 12 }
留意:枚舉類的對象的辦法必需是公有的,不能私有。
枚舉類有如下特性:
1.枚舉類也是一種特殊的Java類。
2.枚舉類中聲明的每一個枚舉值代表枚舉類的一個實例對象。
3.與Java中的普通類一樣,在聲明枚舉類時,也可以聲明屬性、辦法和結構函數,但是枚舉類的結構函數必需為室友的(這點不難了解)。
4.枚舉類也可以完成接口、或基層籠統類。
5.JDK5中擴展了switch語句,它除了可以接納int、byte、char、short外,還可以接納一個枚舉類型。
6.若枚舉類只要一個枚舉值,則可以當做單態設計形式。
上面這樣寫也是單態設計形式。
1 enum Demo{ 2 demo; 3 }
枚舉類的常用辦法
1 public void test(){ 2 //枚舉類的常用辦法name,valueOf,values 3 String name = "B"; 4 Grade g = Grade.valueOf(name);//把字符串轉成枚舉對象 5 System.out.println(g.name());//獲取枚舉對象稱號 6 7 Grade gs[] = Grade.values(); 8 for(Grade g1:gs){ 9 System.out.println(g1.name()); 10 } 11 }