A brief discussion on java enumeration classes (with code)
Introduction to java enumeration class: (java learning video recommendation: java video tutorial)
1. Under what circumstances is the enumeration class used?
Sometimes the objects of a class are limited and fixed. In this case, it is more convenient for us to use an enumeration class.
2. Why not use static constants to replace enumeration classes?
public static final int SEASON_SPRING = 1; public static final int SEASON_SUMMER = 2; public static final int SEASON_FALL = 3; public static final int SEASON_WINTER = 4;
The enumeration class is more intuitive and type safe. Using constants will have the following defects:
1. Type unsafe. If a method requires the parameter of season to be passed in, if a constant is used, the formal parameter will be of type int. The developer can pass in any type of int value, but if it is an enumeration type, it can only be passed into the enumeration class. Contained objects.
2. No namespace. Developers should start with SEASON_ when naming, so that when another developer looks at this code, they will know that these four constants represent seasons.
3. Getting started with enumeration classes
Let’s first look at a simple enumeration class.
package enumcase;public enum SeasonEnum { SPRING,SUMMER,FALL,WINTER; }
enum has the same status as class and interface. Enumeration classes defined using enum inherit java.lang.Enum by default instead of inheriting the Object class. An enumeration class can implement one or more interfaces. All instances of the enumeration class must be displayed on the first line, without using the new keyword and without explicitly calling the constructor. Automatically add public static final modification. Non-abstract enumeration classes defined using enum are decorated with final by default and cannot be inherited. The constructor of an enumeration class can only be private.
4. Introduction to enumeration classes
Attributes and methods can also be defined in enumeration classes, but they are static and non-static.
package enumcase;public enum SeasonEnum { SPRING("春天"),SUMMER("夏天"),FALL("秋天"),WINTER("冬天"); private final String name; private SeasonEnum(String name) { this.name = name; } public String getName() { return name; } }
In fact, when writing an enumeration class instance in the first line, the constructor is called by default, so parameters need to be passed in here, because the parameterless constructor is not explicitly declared and can only be called Constructor with parameters.
The constructor needs to be defined as private, so that such objects cannot be declared elsewhere. Enumeration classes should usually be designed as immutable classes, and their fields should not be changed. This will be safer and the code will be more concise. So we modify the Field with private final.
5. Enumeration class implements interface
An enumeration class can implement one or more interfaces. Like ordinary classes, when implementing an interface, you need to implement all the methods defined in the interface. If it is not fully implemented, then the enumeration class is abstract, but there is no need to explicitly add the abstract modification. Systematization will add it by default.
package enumcase; public enum Operation { PLUS{ @Override public double eval(double x, double y) { return x + y; } }, MINUS{ @Override public double eval(double x, double y) { return x - y; } }, TIMES{ @Override public double eval(double x, double y) { return x * y; } }, DIVIDE{ @Override public double eval(double x, double y) { return x / y; } }; /** * 抽象方法,由不同的枚举值提供不同的实现。 * @param x * @param y * @return */ public abstract double eval(double x, double y); public static void main(String[] args) { System.out.println(Operation.PLUS.eval(10, 2)); System.out.println(Operation.MINUS.eval(10, 2)); System.out.println(Operation.TIMES.eval(10, 2)); System.out.println(Operation.DIVIDE.eval(10, 2)); } }
The Operatio class is actually abstract and cannot create enumerated values, so when declaring enumerated values here, abstract methods are implemented. This is actually the implementation of anonymous inner classes, curly braces Part is a class body. We can take a look at the compiled files.
A total of five class files were generated, which proves that PLUS, MINUS, TIMES, and DIVIDE are instances of Operation's anonymous internal classes.
6. The expression in the switch statement can be an enumeration value
Java5 has added the enum keyword and expanded switch.
package enumcase; public class SeasonTest { public void judge(SeasonEnum s) { switch(s) { case SPRING: System.out.println("春天适合踏青。"); break; case SUMMER: System.out.println("夏天要去游泳啦。"); break; case FALL: System.out.println("秋天一定要去旅游哦。"); break; case WINTER: System.out.println("冬天要是下雪就好啦。"); break; } } public static void main(String[] args) { SeasonEnum s = SeasonEnum.SPRING; SeasonTest test = new SeasonTest(); test.judge(s); } }
Write the enumeration value directly into the case expression without adding the enumeration class as a limitation.
Recommended: java basic tutorial
The above is the detailed content of A brief discussion on java enumeration classes (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
