Operation of Java enumeration class and custom enumeration class and enum declaration and implementation interface
- 2021-08-28 20:05:21
- OfStack
1. Enumerate classes
Note:
Custom enumeration classes are required before JDK 1.5
The new enum keyword in JDK 1.5 is used to define enumeration classes
If the enumeration has only one member, it can be used as an implementation of a singleton pattern
1. Enumerate the properties of a class
1. The properties of enumeration class objects should not be allowed to be changed, so they should be decorated with private final
2. The private final-decorated properties of the enumeration class should be assigned values in the constructor
3. If the enumeration class explicitly defines a constructor with parameters, the enumeration value must also be listed with corresponding passed-in parameters
2. Custom enumeration classes
How to customize the methods of enumerated classes to write in comments
// Custom enumeration class
class Season {
//1. Provides the properties of the class, declared as private final
private final String seasonName;
private final String seasonDesc;
//2. Declare as final Class in the constructor
private Season(String seasonName,String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//3. Calling properties through public methods
public final String getSeasonName() {
return seasonName;
}
public final String getSeasonDesc() {
return seasonDesc;
}
//4. Create an object of an enumeration class : Declares the object of a class as public static final
public static final Season SPRING = new Season(" Spring ", " Spring blossoms ");
public static final Season SUMMER = new Season(" Summer ", " Hot summer days ");
public static final Season FALL = new Season(" Autumn ", " Fruitful ");
public static final Season WINTER = new Season(" Winter ", " Snow gleams white ");
@Override
public String toString() {
return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
}
public void show() {
System.out.println(" This 1 Seasons ");
}
}
Test
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
spring.show();
System.out.println(spring.getSeasonName() +" "+spring.getSeasonDesc());
}
}
3. Enum enumeration class
1. The enumeration class object must be declared on line 1 of the enumeration class.
2. The difference between enumerated classes and ordinary classes:
① The enumeration class defined using enum inherits the java. lang. Enum class by default
② The constructor of enumeration class can only use private access control character
All instances of the enumeration class must be explicitly listed (separated) in the enumeration class; End). The listed instance system automatically adds public static final decorations
3. In JDK 1.5, the object of enumeration class defined by Enum can be used as expression in switch expression, and the name of enumeration value can be directly used in case clause without adding enumeration class as qualification
4. Dome
interface info{
void show();
}
// Enumeration class
enum Season1 implements info{
SPRING(" Spring ", " Spring blossoms "){
public void show() {
System.out.println(" Where is spring? ");
}
},
SUMMER(" Summer ", " Hot summer days ")
{
public void show() {
System.out.println(" Life is like summer flowers ");
}
} ,
AUTUTO(" Autumn ", " Fruitful ")
{
public void show() {
System.out.println(" Autumn is the season for breaking up ");
}
},
WINTER(" Winter ", " Snow gleams white ")
{
public void show() {
System.out.println(" In winter 1 Put the fire ");
}
};
//1. Provides the properties of the class, declared as private final
private final String seasonName;
private final String seasonDesc;
//2. Declare as final Class in the constructor
private Season1(String seasonName,String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//3. Calling properties through public methods
public final String getSeasonName() {
return seasonName;
}
public final String getSeasonDesc() {
return seasonDesc;
}
@Override
public String toString() {
return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
}
// public void show() {
// System.out.println(" This 1 Seasons ");
// }
}
Test
Common methods are written in comments
public class TestEnum {
public static void main(String[] args) {
System.out.println("------------enum Keyword ");
//1.values() Returns an array of enumerated classes
Season1 [] seasons = Season1.values();
for(int i = 0; i < seasons.length;i++) {
System.out.println(seasons[i]);
}
//2.valueOf(String name): Request passed-in formal parameters name Is the name of the enumeration object
// Otherwise: Report java.lang.IllegalArgumentException Anomaly
String str = "SPRING";
Season1 sea = Season1.valueOf(str);
System.out.println(sea);
// Running state of thread
Thread.State[] states = Thread.State.values();
for (int i = 0; i < states.length; i++) {
System.out.println(states[i]);
}
}
}
4. Enumeration classes that implement interfaces
1. The enumeration class that realizes interface is like the common Java class 1. The enumeration class can realize one or more interfaces
2. If each enumeration value is required to exhibit different behavior in the interface method implemented by calling, each enumeration value can be used to implement the method separately
3. The implementation method can be seen in demo above.
Additional: New features of java (enumeration, annotation, interface definition enhancement)
1. Enumerate
1. Enumerative expression
enum ClassName
{
}
2. The nature of enumeration:
Multi-case design pattern
3. Enumerate generation
JKD 1.5 generates an enumeration, which is just a wrapper of one type: an enumeration defined using the enum keyword is essentially equivalent to a class defined by class, inheriting the parent class of java. lang. Enum by default
4. Common methods in enumeration
Get the enumeration name: public final String name ()
Get the enumeration sequence number: public final int ordinal ()
Get all enumerated objects: values (): Return value type: Enum []
5. enum and Enum
enum is a keyword, and an enumeration defined using enum is essentially equivalent to a class inheriting the abstract class Enum.
6. Structures that can be defined in an enumeration
You can have properties, constructors
The enumeration contains other class structures, and the declaration of the enumeration object must be placed in the first row of the enumeration class
Enumeration can realize interface. After realizing interface, enumeration object becomes interface object
2. Note (Annotation): JDK 1.5 adds the two most commonly used features.
Three major annotations provided by JDK1.5: @ Override, @ Deprecated, @ SuppressWarnings
1. Accurate overwrite (@ Override):
If the method is overridden correctly, there will be no compilation errors. If the method name and parameter list are different from the method with the same name in the parent class, an error will be reported.
public class Person
{
@Override // If the method is overridden correctly, there will be no compilation errors. If the method name and parameter list are different from the method with the same name in the parent class, an error will be reported.
public String toString()
{
}
}
2. Expired processing (@ Deprecated):
Acts on classes and methods, indicating that users are not recommended to use this class or method, but it can be used normally.
@Deprecated
public Void fun() {}
3. Suppress warnings (@ SuppressWarnings):
When you invoke some operations that may cause problems, a warning will appear, and you don't want to always prompt warnings. At this time, you can use annotations to suppress warnings.
3. Enhanced interface definition
Before JDK8: Global variables + abstract methods
After JDK8:
1. The common method defined by default, which is called through an object
2. Static methods defined by static, which are called through the interface
interface Imessage
{
public default void fun() // Append a common method with a method body
{
System.out.println("Holle IMessage");
}
public static IMessage getInstance() // Can be called directly by the interface name
{
return new MessageImp();
}
public void print();
}
class MessageImp imlements Imessage
{
@Override
public void print()
{
System.out.println("Holle MessageImp ");
}
}