1 enumeration class
1.1 user defined enumeration class
- The constructor needs to be privatized
- There is no need to provide a set method because the value of the enumeration object is read-only
- The enumerating objects / attributes are decorated with final + static to realize the underlying optimization
- Enumeration object names are usually capitalized
- Enumeration objects can have multiple attributes according to requirements
package com.enum_;
public class Enumeration01 {
public static void main(String[] args) {
System.out.println(Season.SPRING);
System.out.println(Season.SUMMER);
System.out.println(Season.AUTUMN);
System.out.println(Season.WINTER);
}
}
class Season{
private String name;
private String describe;
//
public final static Season SPRING = new Season("spring","warm");
public final static Season SUMMER = new Season("summer","scorching hot");
public final static Season AUTUMN= new Season("autumn","pleasantly cool");
public final static Season WINTER = new Season("winter","cold");
private Season(String name, String describe) {
this.name = name;
this.describe = describe;
}
public String getName() {
return name;
}
public String getDescribe() {
return describe;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", describe='" + describe + '\'' +
'}';
}
}
1.2 enum keyword
- It is equivalent to simplifying the creation of objects into object names (parameters), which must be separated by commas, and the definitions should be placed at the beginning.
- If there are no parameters, it can be simplified to direct definition of object name. For example:
enum Gender{
BOY,GIRL;
}
package com.enum_;
public class Enumeration02 {
public static void main(String[] args) {
System.out.println(Season2.SPRING);
System.out.println(Season2.SUMMER);
System.out.println(Season2.AUTUMN);
System.out.println(Season2.WINTER);
}
}
enum Season2{
SPRING("spring","warm"),
SUMMER("summer","scorching hot"),
AUTUMN("autumn","pleasantly cool"),
WINTER("winter","cold");
private String name;
private String describe;
private Season2(String name, String describe) {
this.name = name;
this.describe = describe;
}
public String getName() {
return name;
}
public String getDescribe() {
return describe;
}
@Override
public String toString() {
return "Season2{" +
"name='" + name + '\'' +
", describe='" + describe + '\'' +
'}';
}
}
1.3 various methods in enum class
- Name: return object name
- ordinal: returns the location number of the object (starting from 0)
- values: returns all constants (an array) in the current class
- Valueof (converts a string to an enumeration object, but requires that the string must be an existing constant name
Season value = Season.valueOf("SPRING");
- toString: return object name
package com.enum_;
public class EnumExercise {
public static void main(String[] args) {
Gender boy = Gender.BOY;
Gender boy2 = Gender.BOY;
System.out.println(boy);
System.out.println(boy == boy2);
System.out.println("==============enum Method test of class==========");
System.out.println("name()function:" + boy.name());
System.out.println("ordinal()function:" +boy.ordinal());
Gender[] genders = Gender.values();
for(Gender gender:genders){
System.out.println("values()function:" +gender);
}
Gender girl = Gender.valueOf("GIRL");
System.out.println("valueOf()function:" +girl);
}
}
enum Gender{
BOY,GIRL;
}
2 annotation
- Basic Annotation
- @Override: it is a method that overrides the parent class. This annotation can only be used for methods
- @Deprecated: used to indicate that a program element is obsolete
- @SuppressWarnings: suppress compiler warnings
2.1 @Override
- Using @ Override indicates that the method overrides the method of the parent class
- The compiler will check whether it is really rewritten. If it is not rewritten, the compiler will report an error
-@Definition of Override
@Target(ElementType.METHOD)//Description can only modify methods. Target is the annotation that modifies annotations, which is called meta annotation
@Retention(RetentionPolicy.SOURCE)
public @interface Override {//@interface indicates that it is an annotation class
}
2.2 @Deprecated
- Used to indicate that a program element is obsolete, which does not mean it cannot be used
- You can modify methods, classes, fields, packages, and parameters
2.3 @SuppressWarnings
- If you don't want to see compiler related warnings, you can use this annotation with related parameters
2.4 yuan notes
- A comment that modifies an annotation
2.4.1 @Retention
- Three values of Retention
- RententionPolicy.SOURCE: after the compiler uses it, it directly discards the annotation of this strategy
- RententionPolicy.CLASS: the compiler records the annotations in the class file. When running, the JVM will not keep them
- RententionPolicy.RUNTIME: the compiler records the annotation in the class file. When running, the JVM will keep it, and the program can get the annotation through reflection
2.4.2 @Target
- Used to specify which program elements can be modified by the modified annotation
2.4.3 @Documented
- It is used to specify that the annotation modified by the annotation will be extracted into a document by javadoc tool, that is, the annotation can be seen when the document is generated
2.4.4 @Inherited
- The annotation class modified by the modified annotation has inheritance, that is, a class is modified by the annotation modified by @ Inherited, and its subclass automatically obtains the annotation.