Principle analysis of Java enumeration class interface instance
This article mainly introduces the principle analysis of Java enumeration class interface examples. The example code is introduced in great detail, which has a certain reference value for everyone's study or work. Friends in need can refer to it
Enumeration classes can implement one or more interfaces. Just like an ordinary class implementing an interface, when enumerating a class implementing an interface, you need to implement the methods contained in the interface.
If you need each enumeration value to behave differently when calling the same method, you can make each enumeration value in {...} Implement your own business logic in anonymous blocks.
public interface IGradeHandler { String getGrade(String studentName); }
public enum GradeHandlerEnum implements IGradeHandler { //five grade levels A B C D E A("90~100") { @Override public String getGrade(String studentName) { return studentName + " Outstanding"; } },E("0~59") { @Override public String getGrade(String studentName) { return studentName + " Fail"; } }; private String score; GradeHandlerEnum(String score) { this.score = score; } public String getscore() { return this.score; } public static void main(String[] args) { GradeHandlerEnum a = GradeHandlerEnum.A; GradeHandlerEnum e = GradeHandlerEnum.valueOf("E"); System.out.println( a.getGrade("Lucy")); System.out.println( e.getGrade("Tom")); } }
For the convenience of testing, the example defines only two levels a and E, and the execution results are as follows:
Lucy Outstanding Tom Fail
Decompile enumeration class
The simple use of enumeration types is introduced in Java enumeration abstract method instance parsing. Now it is necessary to understand its basic implementation principle. In fact, after the enum class is created and compiled with the keyword enum, the compiler will generate some related classes that inherit Java. Net from the Java API Lang. enum class, that is, the enumeration class created through the keyword enum is actually a class type after compilation, and this class inherits from Java. Enum Lang. enum class.
IGradeHandler.class GradeHandlerEnum.class GradeHandlerEnum$1.class GradeHandlerEnum$2.class
Compile gradehandlerenum. Java using javac Java file, generate four class files, among which, gradehandlerenum Class is an enumeration type, which verifies that after defining an enumeration class with the keyword enum and compiling it, the compiler will automatically help us generate an enumeration related class. The main class files decompiled with JD GUI are as follows:
import java.io.PrintStream; public enum GradeHandlerEnum implements IGradeHandler { // 编译后生成两个class文件GradeHandlerEnum$1和GradeHandlerEnum$2 A("90~100"),E("0~59"); private String score; //私有构造函数 private GradeHandlerEnum(String paramString) { this.score = paramString; } public String getscore() { return this.score; } public static void main(String[] paramArrayOfString) { //实例化枚举实例,变量名由系统生成 GradeHandlerEnum localGradeHandlerEnum1 = A; GradeHandlerEnum localGradeHandlerEnum2 = valueOf("E"); System.out.println(localGradeHandlerEnum1.getGrade("Lucy")); System.out.println(localGradeHandlerEnum2.getGrade("Tom")); } } //枚举值A对应的枚举类 enum GradeHandlerEnum$1{ GradeHandlerEnum$1(String paramString1) { super(paramString,paramInt,paramString1,null); } public String getGrade(String paramString) { return paramString + " Outstanding"; } } //枚举值B对应的枚举类 enum GradeHandlerEnum$2{ GradeHandlerEnum$2(String paramString1) { super(paramString,null); } public String getGrade(String paramString) { return paramString + " Fail"; } }
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.