Java Enumeration Classes(2009-01-13 11:37:01)reproduced
| Tags: java enum enumeration it |
Category: Programming |
1. Create one of the simplest enumeration types
Creating an enumeration type to use the enum keyword implies that the type created is a subclass of the Java.lang.Enum class, which is an abstract class. Where the enumeration type conforms to the common pattern and Class Enum<E extends Enum<E>> E represents the name of the enumeration type. Each value of the enumeration type is mapped to the protected Enum(String name, int ordinal) constructor, where the name of each value is converted to a string, and the ordinal setting indicates the order in which this setting was created.
public enum size{
SMALL,
MEDIUM,
LARGE;
}
Three constructors are actually called:
New Enum<size> ("SMALL", 0);
New Enum<size> ("MEDIUM", 1);
New Enum<size> ("LARGE", 2);
The name and order values of the values can be obtained by name ()/tostring (), ordinal (), respectively.
The self-built enumeration class automatically obtains two methods, values () and valueof (String name) In addition to the method that inherits the enum class. You do not need to define your own to use it directly. I'm not quite sure about the principles, but the people who are in trouble understand me.
VALUES () can be used to traverse the value in an enumeration class, and ValueOf (String name) can be used to obtain a specific enumeration value by name.
Get the Enumeration class value method: There are three ways to see the code.
Public class Mostsimpleenum {
public enum size{
SMALL,
MEDIUM,
LARGE;
}
public static void Main (string[] args) {
Two ways to get enum types
Size S1 = Size.small;
Size s2 = enum.valueof (Size.class, "SMALL");
Size s3 = size.valueof ("MEDIUM");//1
Use ' = ' directly to compare two enum types for equality
if (Size.large = = enum.valueof (Size.class, "LARGE")) {
System.out.println ("Size.large = = enum.valueof (Size.class, \" large\ ")");
}
As a result, weight lifting carried the Equals method, but the comparison method was the same as
System.out.println ("Size.MEDIUM.equals (enum.valueof (Size.class, \" Medium\ ")):" +
Size.MEDIUM.equals (enum.valueof (Size.class, "MEDIUM"));
Iterate through all the members of an enumeration type
For (Size s:size.values ()) {//2
1. The defined ordinal number 2. Name of the definition 3. Overloaded in enum, same as name ()
System.out.println (s.ordinal () + "" +s.name () + "" +s.tostring ());
}
Analysis of two methods not defined in abstract class enum the origins of Values () and valuesof ()
Analyzeenum (S1);
An abstract class that defines the default inheritance of enumeration classes Java.lang.Enum
There are two special methods that we don't define, and the result is a class that comes from our custom definition
Previously used, 1.valueOf (String name), 2.values ()
The enumeration class automatically includes these two previously defined methods
System.out.println ("get" +s3);
}
public static void Analyzeenum (Enum e) {
system.out.println ("Class:" +e.getclass (). GetName ());
system.out.println ("Declear class:" +e.getclass (). Getdeclaringclass ());
system.out.println ("Super Class:" +e.getclass (). Getsuperclass (). GetName ());
class<?>[] interfaces = E.getclass (). Getinterfaces ();
for (Class c:interfaces) {
system.out.println ("Implement Interdace:" +c.getname ());
}
Method[] methods = E.getclass (). GetMethods ();
for (Method m:methods) {
System.out.println ("Mehtods:" +m.getname () + "From:" +m.getdeclaringclass ());
}
}
}
2. Create a complex point enumeration class week,in this enumeration class we add our own defined members, constructors, methods to it. Note that although there is a constructor in the enum, but we cannot call it in the custom enumeration class, it is not allowed to inherit java.lang.Enum directly. The constructor we define ourselves can only be defined as private or package-protected, nor can we call this custom constructor.
public enum Week {
Defines the value of an enumeration class
Monday ("MON", "Monday"),
Tuesday ("TUE", "Tuesday"),
Wednesday ("WED", "Wednesday"),
Thursday ("THU", "Thursday"),
Friday ("FRI", "Friday"),
Saturday ("SAT", "Saturday") {
@Override
public Boolean isrest () {
return true;
}
},
Sunday ("SUN", "Sunday") {
@Override
public Boolean isrest () {
return true;
}
};
Private String abbreviation = "";//abbreviation
Private String Chinesename = "";//Chinese name
Define your own constructor
Private Week (String abbreviation,string chinesename) {
this.abbreviation = abbreviation;
This.chinesename = Chinesename;
}
public String abbreviation () {
return abbreviation;
}
Public String Getchinesename () {
return chinesename;
}
Saturday and Sunday should return true, this method is overloaded in values in Saturday and Sunday
public Boolean isrest () {
return false;
}
Reload, make a slight change to it
@Override
Public String toString () {
Return This.getclass (). GetName () + "." +this.name ();
}
}
Test Week
public class Weektest {
public static void Main (string[] args) {
For (Week week:Week.values ()) {
System.out.println ("-----------------------------------------");
System.out.println ("Ordinal ():" +week.ordinal ());
System.out.println ("Name ():" +week.name ());
System.out.println ("Getchinesename ():" +week.getchinesename ());
SYSTEM.OUT.PRINTLN ("abbreviation ():" +week.abbreviation ());
System.out.println ("Isrest ():" +week.isrest ());
System.out.println ("toString ():" +week.tostring ());//If this method is not overloaded, the function is the same as the name ().
System.out.println ("Getdeclaringclass ():" +week.getdeclaringclass ());
}
}
}
By the way, the method of inheriting or implementing an interface is the same as the generic Java class.
Java Enumeration Class-compare two enumerated objects Java enumeration Class-collection of enumerations
Reference: Tame Tiger: Drill down on enum typeshttp://www-128.ibm.com/developerworks/cn/java/j-tiger04195/new features of Java 1.5 enum usage http://conkeyn.javaeye.com/blog/232757
Java Enumeration Classes