In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to analyze the Java enumeration class interface example principle, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
Enumerated classes can implement one or more interfaces. Just like an ordinary class implements an interface, when enumerating a class to implement an interface, you need to implement the methods contained in that interface.
If you need each enumerated value to behave differently when the same method is called, you can have each enumerated value in {.} implement its own business logic in an anonymous block.
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"));}}
To facilitate testing, only two levels An and E are defined in the example, and the execution results are as follows:
Lucy OutstandingTom Fail
Reverse compilation enumeration class
Now it is necessary to understand its basic implementation principle. In fact, after you create an enumerated class using the keyword enum and compile it, the compiler generates related classes that inherit the java.lang.Enum class in Java API, that is, the enumerated class created by the keyword enum is actually a class type after compilation and inherits from the java.lang.Enum class.
IGradeHandler.classGradeHandlerEnum.classGradeHandlerEnum$1.classGradeHandlerEnum$2.class
After compiling the GradeHandlerEnum.java file with javac, we generate four class files, where GradeHandlerEnum.class is the enumerated type, which verifies that after we define the enumerated class using the keyword enum and compile it, the compiler will automatically help us generate an enumerated class. The main class files decompiled with jd-gui are as follows:
Import java.io.PrintStream;public enum GradeHandlerEnum implements IGradeHandler {/ / compiles two class files GradeHandlerEnum$1 and GradeHandlerEnum$2 A ("90,100"), E ("0,59"); private String score; / / private constructor private GradeHandlerEnum (String paramString) {this.score = paramString;} public String getScore () {return this.score;} public static void main (String [] paramArrayOfString) {/ / instantiate the instance, and the variable name is generated by the system GradeHandlerEnum localGradeHandlerEnum1 = A; GradeHandlerEnum localGradeHandlerEnum2 = valueOf ("E") System.out.println (localGradeHandlerEnum1.getGrade ("Lucy")); System.out.println (localGradeHandlerEnum2.getGrade ("Tom"));}} / / enumerated class enum GradeHandlerEnum$1 {GradeHandlerEnum$1 (String paramString1) {super (paramString, paramInt, paramString1, null);} public String getGrade (String paramString) {return paramString + "Outstanding";}} / / enumerated value B corresponding to enum GradeHandlerEnum$2 {GradeHandlerEnum$2 (String paramString1) {super (paramString, paramInt, paramString1, null) } public String getGrade (String paramString) {return paramString + "Fail";}}
The above content is how to analyze the Java enumeration class interface instance principle, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 241
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.