Related applications for enumerations in Java

Source: Internet
Author: User

Package example6;

Import Org.junit.Test;
/*1. What is an enumeration?
* Need to be within the Indigo range, this value can only be one of the range
* Use enum keyword enum
* There are also construction methods and abstract methods in enumerations
* Abstract methods also need to be overridden in the instance.
* */

public class Enumlist {
The object that knows the enumeration, gets the name of the enumeration and the subscript
@Test
public void Test2 () {
Knowing the names of enumerations, getting enumerated objects and subscripts
String name= "RED";
The object that gets the enumeration
Color red=color.valueof (name);
Get the subscript for the enumeration
int index=red.ordinal ();
SYSTEM.OUT.PRINTLN (index);
}

@Test
public void Test3 () {
Know the subscript for an enumeration, and find the object and name of the enumeration
int index=1;
Get Enum Object
Color[] Green =color.values ();
Color Greens=green[index];
Get enumeration Name
String Name=greens.name ();
SYSTEM.OUT.PRINTLN (name);
}


@Test
public void Test1 () {
Get Enum Object
The object that knows the enumeration, gets the name of the enumeration and the subscript
Color red=color.red;
Color Green=color.green;
Color Yellow=color.yellow;
Get enumeration Name
String Name=red.name ();
String Name1=green.name ();
String Name2=yellow.name ();
Get enumerated subscript n
int index=red.ordinal ();
int index1=green.ordinal ();
int index2=yellow.ordinal ();
System.out.println (name+ "= = =" +index);
System.out.println (name1+ "= = =" +index1);
System.out.println (name2+ "= = =" +index2);
}
}
Enum color{
Red,green,yellow;
}

Related applications for enumerations in Java

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.