Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the abstract method instance of Java enumeration

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to parse Java enumeration abstract method examples, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Demand background

The requirement has identified several fixed constant values, and each constant value has the same behavior, but the implementation details are different. It is recommended to use enumerated abstract method, which has the advantages of clear structure and easy to expand.

Enumerated classes implement abstract methods

Like regular abstract classes, the abstract class allows us to define an abstract method for it, and then make each enumerated instance implement that method in order to produce a different behavior. Note that the enumerated class keyword is not necessary for enumerated classes, so take a chestnut:

Public enum GradeEnum {/ / Test scores are divided into five grades A ("90,100") {@ Override public String getGrade (String studentName) {return studentName + "excellent";}}, B ("80,89") {@ Override public String getGrade (String studentName) {return studentName + "good";}}, C ("70th 79") {@ Override public String getGrade (String studentName) {return studentName + "medium" + getScore () }, D ("60,69") {@ Override public String getGrade (String studentName) {return studentName + "pass";}}, E ("0,59") {@ Override public String getGrade (String studentName) {return studentName + "very poor";}; private String score; private GradeEnum (String score) {this.score = score;} public String getScore () {return this.score;} public abstract String getGrade (String studentName);}

The abstract keyword is not added when GradeEnum is defined.

You can think of the enum class as an ordinary class, and enumerated classes can define some properties and methods, except that enum cannot inherit other classes using the extends keyword, because enum already inherits java.lang.Enum (java is a single inheritance).

The member method can directly manipulate the member variable, such as score, and get the return result. The static method traverses each instance and uses its member variable to calculate the return result. The method process is calculated according to the member attribute.

If we want each instance to have a completely different method implementation that does not depend on member variables, then we need to define abstract methods that use {.} anonymous blocks to implement abstraction in anonymous blocks. Such as the abstract method getGrade in the above example. You can also achieve the same goal by enumerating classes that implement interfaces.

After reading the above, do you have any further understanding of how to parse abstract method instances of Java enumerations? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report