Enumeration class
When we need to define a set of constants, we often use enumeration classes.
Usage 1: Constant
Before JDK1.5, we defined constants as: public static fianl.... Now that's good, with enumerations, related constants can be grouped into an enumeration type, and enumerations provide more methods than constants.
public enum ColorEnum {
RED, GREEN, BLANK, YELLOW
}
Usage 2: switch
The switch statement before JDK1.6 only supports int, char, enum types. Using enumerations can make our code more readable.
enum Signal {
GREEN, YELLOW, RED
}
public class TrafficLight {
Signal color = Signal.RED;
public void change() {
switch (color) {
case RED:
color = Signal.GREEN;
break;
case YELLOW:
color = Signal.RED;
break;
case GREEN:
color = Signal.YELLOW;
break;
}
}
}
Usage 3: Add a new method to the enumeration
If you plan to customize your own method, you must add a semicolon at the end of the enum instance sequence. And Java requires that an enum instance must be defined first.
public enum Color {
RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
// 成员变量
private String name;
private int index;
// 构造方法
private Color(String name, int index) {
this.name = name;
this.index = index;
}
// 普通方法
public static String getName(int index) {
for (Color c : Color.values()) {
if (c.getIndex() == index) {
return c.name;
}
}
return null;
}
// get set 方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
Usage five: implement the interface
All enumerations are inherited from the java.lang.Enum class. Since Java does not support multiple inheritance, enumeration objects can no longer inherit other classes.
public interface Behaviour {
void print();
String getInfo();
}
public enum Color implements Behaviour{
RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
// 成员变量
private String name;
private int index;
// 构造方法
private Color(String name, int index) {
this.name = name;
this.index = index;
}
//接口方法
@Override
public String getInfo() {
return this.name;
}
//接口方法
@Override
public void print() {
System.out.println(this.index+":"+this.name);
}
}
Usage six: use interface to organize enumeration
public interface Food {
enum Coffee implements Food{
BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO
}
enum Dessert implements Food{
FRUIT, CAKE, GELATO
}
}
Usage Seven: About the use of enumerated collections
java.util.EnumSet
The sum java.util.EnumMap
is two enumerated collections. EnumSet ensures that the elements in the set are not repeated; the key in EnumMap is of the enum type, and the value can be of any type. Regarding the use of these two collections, I won't repeat them here, you can refer to the JDK documentation.
For the implementation details and principles of enumeration, please refer to:
Reference materials: "ThinkingInJava" Fourth Edition
annotation
Introduction
- Starting from JDK 5.0, Java has added support for MetaData, which is
Annotation - Annotations are actually special tags in the code. These tags can be read during compilation, class loading, and runtime, and corresponding processing can be performed. By enabling Annotation, programmers can embed some supplementary information in the source file without changing the original logic. Code analysis tools, development tools, and deployment tools can be verified or deployed through these supplementary information.
Common annotations
@author indicates the author who developed this type of module, used between multiple authors, split
@version indicates the version of this type of module
@see reference steering, that is, the relevant topic
@since from which version
@param was added to a parameter in the method If there is no parameter, you cannot write
@return to describe the return value of the method. If the return value type of the method is void, you cannot write
@exception to describe the exceptions that the method may throw. If the method does not explicitly throw with throws The exception cannot be written in it
@Override: Limit overriding the parent class method, this annotation can only be used for the method
@Deprecated: Used to indicate that the modified element (class, method, etc.) is out of date. Usually because the modified structure is dangerous or there is a better choice
@SuppressWarnings: Suppress compiler warnings
Custom annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation{
String value() default "myannotion";
}
Meta annotations in JDK
- Define a new Annotation type using the @interface keyword
- Custom annotations automatically inherit the java.lang.annotation.Annotation interface
- The member variables of Annotation are declared in the form of a parameterless method in the Annotation definition. The method name and return value define the name and type of the member. We call it configuration parameters. The type can only be
an array of eight basic data types, String type, Class type, enum type, Annotation type, and all the above types. - You can specify the initial value of the Annotation member variable when you define it, and you can use the default keyword to specify the initial value of the member variable.
- If there is only one parameter member, it is recommended to use the parameter name value
- If the defined annotation contains configuration parameters, then the parameter value must be specified when using it, unless it has a default
value. The format is "parameter name = parameter value". If there is only one parameter member and the name is value,
"value=" can be omitted - Annotations without member definitions are called tags; Annotations containing member variables are called metadata
Annotations