Learn from teacher Wang Enumeration (iii): Enumeration API
Teacher: Wang Shaohua QQ Group No.: 483773664
First, enumerate the class API
The enumeration classes declared in Java are children of the Java.lang.Enum class that inherit all the methods of the enum class. Common methods:
Name (): Returns the names of this enumeration constant
Ordinal (): Returns the ordinal of an enumerated constant (its position in the enumeration declaration, where the initial constant ordinal is zero
valueof (Class enumclass, String name): Returns an enumeration constant of the specified enumeration type with the specified name
valueof (String name): Returns the current enumeration type with the specified name
VALUES () This method, although not found in the JDK document, has the method for each enumeration class, which iterates through all the enumeration values of the enumeration class
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/88/wKioL1dYEkmQWrmFAAA2A_9itvI538.png "alt = "Wkiol1dyekmqwrmfaaa2a_9itvi538.png"/>
Second, enumeration class API instance
Public class enumtest { public static void main (String[] args) { //Returns the name of the enumeration type system.out.println (Grade.a.name ()); // Returns the subscript system.out.println (Grade.a.ordinal ()) of the enumeration type; //to get an enumerated object through a string String Value= "D"; //. Gets the current enumeration grade grade = grade.valueof (value); System.out.println (Grade.name () +grade.getvalue () +grade.getlocstring ()); //second, through the parent class grade grade2 = enum.valueof (Grade.class, value); &Nbsp; system.out.println (Grade2.name () +grade2.getvalue () + Grade2.getlocstring ()); //get all the instance objects Grade[] grades = Grade.values (); for (grade g : grades) { system.out.println ( G.getlocstring () + "----"); } //get the object by subscript int index = 2; grade[] grades2 = grade.values (); Grade grade3 = grades2[index]; system.out.println (index+ "----" +grade3.name()); }}
This article is from "Learn programming with Mr. Wang" blog, please be sure to keep this source http://teacherwang.blog.51cto.com/10946447/1787564
Learn from teacher Wang Enumeration (iii): Enum class API