Learn from teacher Wang Enumeration (iii): Enum class API

Source: Internet
Author: User

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

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.