Java _ enumeration class, java enumeration class

Source: Internet
Author: User

Java _ enumeration class, java enumeration class

Enumeration class

When the enumeration class is not available, only the following code can be written:

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 ("score input error"); 19} 20 this. grade = grade; 21} 22}

The first line of code uses a regular expression.

 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 }

This code is a bit bad, it is to assign a value, but an error is reported only when the program is running. How can I get an error when writing code? See the following code.

 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 }

Define a class that you use, and assign only the specified value when you assign a value. errors are returned for other values.

 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 }

This method is too complex. The following uses enumeration. Only three lines can replace the 21-29 lines of code.

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 {// also defines A class of 32 A, B, C, D, E; // corresponding to Grade class Object 33}

Use the enumeration class to replace line 21-29 code.

 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 }

This method is better, but how to encapsulate more information in the enumeration class? See the following code.

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 // how to encapsulate more information for enumeration (How to Make enumeration objects have their own attributes) 32 enum Grade {// also defines A Class 33 A ("100-90"), B ("89-80"), C ("79-70 "), D ("69-60"), E ("59-0"); // corresponding to the Grade class Object 34 35 private String value; 36 private Grade (String value) {37 this. value = value; 38} 39 public String getValue () {40 return value; 41} 42}

You can encapsulate scores and add attributes.

 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 }

How can we encapsulate methods in enumeration classes? See the following code.

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 // how to make the enumerated object have its own attributes and their respective Methods 32 enum Grade {// also defines A Class 33 A ("100-90 ") {34 public String toLocalString () {35 return ""; 36} 37}, B ("89-80") {38 public String toLocalString () {39 return "good"; 40} 41}, C ("79-70") {42 public String toLocalString () {43 return "medium"; 44} 45 }, D ("69-60") {46 public String toLocalString () {47 return "general"; 48} 49}, E ("59-0 ") {50 public String toLocalString () {51 return "difference"; 52} 53}; // corresponding to the Grade class Object 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}

Returns the Chinese Representation of the score.

 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 }

Note: The methods of enumeration objects must be private and not public.

 

Enumeration classes have the following features:

1. Enumeration classes are also special Java classes.

2. Each enumerated value declared in the enumeration class represents an Instance Object of the enumeration class.

3. Like common classes in Java, attributes, methods, and constructors can also be declared when enumeration classes are declared, but the constructors of enumeration classes must be roommates (this is not hard to understand ).

4. Enumeration classes can also implement interfaces or basic abstract classes.

5. The switch statement is extended in JDK 5. It can receive int, byte, char, and short types as well as an enumeration type.

6. If the enumeration class has only one enumerated value, it can be used as a single-State design mode.

The following is also a single-State design mode.

1 enum Demo{2     demo;3 }

 

Common enumeration methods

1 public void test () {2 // name, valueOf, values 3 String name = "B"; 4 Grade g = Grade. valueOf (name); // converts a string to an enumerated object 5 System. out. println (g. name (); // obtain the name of the enumerated object 6 7 Grade gs [] = Grade. values (); 8 for (Grade g1: gs) {9 System. out. println (g1.name (); 10} 11}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.