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

Example Analysis of enumerated classes in Java

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the example analysis of enumerated classes in Java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

I. the use of enumerated classes

1. The understanding of the enumerated class: there are only a limited number of objects in the class. We call this class enumerated.

two。 When you need to define a set of constants, it is strongly recommended that you use the enumeration class

3. If there is only one object in the enumeration class, it can be used as a way to implement the singleton pattern.

How to define enumerated classes

Method 1: before JDK5.0, customize the enumeration class

Public class SeasonTest {public static void main (String [] args) {Season spring = Season.SPRING; System.out.println (spring);}} class Season {/ / 1. Declare the properties of the Season object: private final decorates private final String seasonName; private final String seasonDesc; / / 2. Privatize the constructor of the class and assign the object attribute private Season (String seasonName, String seasonDesc) {this.seasonName = seasonName; this.seasonDesc = seasonDesc;} / / 3. Provide the current enumeration class for each object: public static final public static final Season SPRING = new Season ("spring", "spring blossoms"); public static final Season SUMMER = new Season ("summer", "hot summer"); public static final Season AUTUMN = new Season ("autumn", "autumn cool"); public static final Season WINTER = new Season ("winter", "ice and snow") / / 4. Other claim 1: get the property public String getSeasonName () {return seasonName;} public String getSeasonDesc () {return seasonDesc;} / / 4 of the enumerated class object. Other request 2: provide toString () @ Override public String toString () {return "Season {" + "seasonName='" + seasonName +'\'+ ", seasonDesc='" + seasonDesc +'\'+'}';}}

Method 2: after JDK5.0, you can use the enum keyword to define enumerated classes

Public class SeasonTest1 {public static void main (String [] args) {Season1 summer = Season1.SUMMER; System.out.println (summer); System.out.println (Season1.class.getSuperclass ()); / / java.lang.Enum / / values (): Season1 [] values = Season1.values (); for (int I = 0; I < values.length; ivalues +) {System.out.println (values [I]) } / / valueOf (String objName): returns the object of objName when the object in the enumeration class. Season1 winter = Season1.valueOf ("WINTER"); / / if there is no enumerated class object for objName, throw an exception: IllegalArgumentException// Season1 winter = Season1.valueOf ("WINTER1"); System.out.println (winter); / / winter.show ();} / / use the enum keyword to define the enumerated class enum Season1 {/ / 1. Provide objects of the current enumeration class, using ", separated, end objects"; "end SPRING (" spring "," spring blossoms "), SUMMER (" summer "," summer heat "), AUTUMN (" autumn "," autumn crisp "), WINTER (" winter "," ice and snow "); / / 1. Declare the properties of the Season object: private final decorates private final String seasonName; private final String seasonDesc; / / 2. Privatize the constructor of the class and assign the object attribute private Season1 (String seasonName, String seasonDesc) {this.seasonName = seasonName; this.seasonDesc = seasonDesc;} / / 4. Other claim 1: get the property public String getSeasonName () {return seasonName;} public String getSeasonDesc () {return seasonDesc;} of the enumerated class object

Implementation of an interface using enumerated classes defined by the enum keyword

Case 1: implement the interface and implement abstract methods in the enum class

Case 2: let the objects of the enumerated class implement the abstract methods in the interface respectively

Public class SeasonTest1 {public static void main (String [] args) {/ / values (): Season1 [] values = Season1.values (); for (int I = 0; I < values.length; ivalues +) {System.out.println (values [I]); values [I] .show ();} interface Info {void show () } enum Season1 implements Info {SPRING ("spring", "spring blossoms") {@ Override public void show () {System.out.println ("where is spring") }}, SUMMER ("summer", "summer heat") {@ Override public void show () {System.out.println ("Ningxia") }}, AUTUMN ("autumn", "autumn is crisp") {@ Override public void show () {System.out.println ("autumn is not coming back") }, WINTER ("Winter", "Ice and Snow") {@ Override public void show () {System.out.println ("about Winter");}; private final String seasonName; private final String seasonDesc Private Season1 (String seasonName, String seasonDesc) {this.seasonName = seasonName; this.seasonDesc = seasonDesc;} public String getSeasonName () {return seasonName;} public String getSeasonDesc () {return seasonDesc }} this is the end of the article on "sample Analysis of enumerated classes in Java". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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