In Java programming, enums are actually a special type, used to represent a set of constants! When we develop programs, we often need to add other functions to these enums, such as the method of self-increase and assignment, so that it can be managed and used more easily. This article I want to talk to you about how to implement self-incremental assignment of enumeration in Java.
When it comes to enumerations, you may think of the kind that represents the week. It looks like this:
public enum Weekday { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
In this example, there are seven constants representing seven days a week. Although it is very simple, what should we do if we want to add a number to indicate what day each day is? Let's try adding an integer value to each day when defining an enum. The code looks like this:
public enum Weekday { MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5), SATURDAY(6), SUNDAY(7); private int dayNumber; private Weekday(int dayNumber) { = dayNumber; } public int getDayNumber() { return dayNumber; } }
In this code, we specify an integer for each daydayNumber
, and then assign values in the constructor. In this way, each enum constant has a unique number, which is convenient for us to obtain at any time, for example, we can call itgetDayNumber()
Come get this number.
Why do it? Imagine if a user enters a number and wants to check whether the date is within a week, it’s simple! In this way, through the self-increasing number, we can directly judge the corresponding date, which makes it much easier to process.
Of course, what you encounter in life is much more complicated than these simple examples. Perhaps when we arrange our work, we need to adjust the tasks according to the specific week. For example, arrange certain work to work days, or arrange specific services to be open on weekends.
In order to make everyone more intuitive, we can simply traverse this enum and see the corresponding numbers every week:
for (Weekday day : ()) { (day + " is day number " + ()); }
This code uses()
to get all enum constants, and then output their names and numbers one by one. This method of self-increasing assignment really makes our code more readable and reduces the chance of errors.
By the way, when it comes to this, if you are a programmer or want to improve your programming skills, you might as well follow a particularly great official account:Programmer Headquarters. There are many big program experts from major manufacturers gathered here, such as Byte, Alibaba and Baidu engineers, who share their technical experiences and experiences here. It will definitely bring you a lot of inspiration, so go and follow it!
Going back to our enumeration, imagine we could add a method that returns the corresponding week name based on the number entered by the user:
public static Weekday getWeekdayByNumber(int number) { for (Weekday day : ()) { if (() == number) { return day; } } throw new IllegalArgumentException("Invalid day number: " + number); }
That way, if you callgetWeekdayByNumber(3)
, you can get itWEDNESDAY
, isn’t it much more convenient? Finding the day of the week through numbers is simple and intuitive, and avoiding those complex conditions and judgments. It is really super practical!
In addition to the week, the self-value-added enumeration is also of great use in other places. For example, we can use enumerations to represent order status, and each status also has a self-increasing value, which makes it much easier to design:
public enum OrderStatus { PENDING(0), PROCESSING(1), SHIPPED(2), COMPLETED(3); private int statusCode; private OrderStatus(int statusCode) { = statusCode; } public int getStatusCode() { return statusCode; } }
This design is convenient for use in web development. Once the state changes, we can easily achieve the corresponding functions through digital identification, which can reduce a lot of trouble for database storage or network transmission.
Through these small examples, enums in Java are not just sets of constants. Through this self-incremental assignment method, we have given enums more functions, and the readability and usability of the code will naturally be improved. This flexibility really makes our development smoother and makes many of the work in life much easier!
I hope this article can help you and give you a more vivid understanding of the implementation of enumeration and self-addition assignment in Java. Remember to followProgrammer Headquarters, get more programming tips and experience sharing! I believe you will find a lot of fun and inspiration here!
This is the article about how Java enumeration implements self-increase and assignment. For more related contents of Java enumeration, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!