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