enumeration
Custom class implementation enumeration
Steps:
1. There is no need to provide setxxx method, because the properties of enumerated objects only need to be read.
2. Use final + static modification on enumeration objects to realize underlying optimization.
3. Enumeration object names are usually all capitalized, which is a naming convention.
4. The constructor is privatized, but the object is exposed.
package Enum; public class DefineMyself { public static void main(String[] args) { System.out.println(Season.SPRING); System.out.println(Season.SUMMER); } } class Season{ private String name ; private String weather; public static final Season SPRING = new Season("spring","warm"); public static final Season SUMMER = new Season("summer","scorching hot"); private Season(String name, String weather) { this.name = name; this.weather = weather; } public String getName() { return name; } public String getWeather() { return weather; } @Override public String toString() { return "Season{" + "name='" + name + '\'' + ", weather='" + weather + '\'' + '}'; } }
Enum using enum keyword
1. When we use enum keyword to create an enumeration class, we will inherit enum class by default, and this class uses final decoration.
2. Traditional public static final Season SPRING = new Season("spring", "warm"); It becomes SPRING("spring", "warm"), and we should pay attention to which constructor it calls.
3. If the enumeration object created by the parameterless constructor is called, the argument list and parentheses can be omitted.
The enumeration object must be placed on the first line of the enumeration class.
package Enum; public class DefineMyself { public static void main(String[] args) { System.out.println(Season.SPRING); System.out.println(Season.SUMMER); } } enum Season{ SPRING("spring","warm"), SUMMER("summer","scorching hot"); public String name ; public String weather; private Season(String name, String weather) { this.name = name; this.weather = weather; } public String getName() { return name; } public String getWeather() { return weather; } @Override public String toString() { return "Season{" + "name='" + name + '\'' + ", weather='" + weather + '\'' + '}'; } }
Note that when enum keyword is used to create enumeration class, the object still has static and final decoration
Examples of common methods of enum
values: returns all constants in the current enumeration class. Returns an array.
class Untitled { public static void main(String[] args) { Season season = Season.SPRING; System.out.println(season.name()); System.out.println(season.ordinal());//Output 0 Season[] seasons = Season.values(); for(Season s1: seasons){ System.out.println(s1);//Output SPRING and SUMMER } Season value = Season.valueOf("SPRING"); System.out.println(value);//Output SPRING System.out.println(season.compareTo(Season.SUMMER));//0-1 = - 1, output - 1 } } enum Season{ SPRING("spring","warm"),SUMMER("summer","scorching hot"); private String name; private String weather; private Season(String name,String weather){ this.name = name; this.weather = weather; } }
Use details
1. After using the Enum keyword, you can no longer inherit other classes, because Enum implicitly inherits Enum, while JAVA is a single inheritance mechanism.
2. Like ordinary classes, enumeration classes can implement interfaces.
enum Class name implements Interface 1, interface 2{};
annotation
Understanding of annotations
Annotation, also known as metadata, is used to modify the data information of interpretation, packages, classes, methods, attributes, constructors and local variables.
Like annotations, they do not affect the logic of the program, but annotations can be compiled and run, which is equivalent to supplementary information embedded in the code.
When using Annotation, add the @ symbol in front of it, and use Annotation as a modifier to decorate the program elements it supports.
Three basic annotations:
1.@Override: defines a method that overrides the parent method. This annotation can only be used for methods.
Note: if @ Override is used, the next method must Override the parent method. If not, the compiler will report an error.
//@Override source code @Target(ElementType.METHOD) @Retention(RetentionPolicy.SOURCE) public @interface Override { }
Note: @ interface represents an annotation class, not an interface.
@Target is an annotation that modifies an annotation, called a meta annotation.
2.@Deprecated: used to indicate that a program element (class, method, etc.) is obsolete.
package annotation_; public class deprecated_ { public static void main(String[] args) { //A horizontal line appears on a, indicating that it is outdated and not recommended, but it can still be used A a = new A(); } } @Deprecated class A{ public int n1; }
@Deprecated can be used for compatibility and transition between old and new versions of JDK.
3.@SuppressWarnings: suppress compiler warnings.
package annotation_; import java.util.ArrayList; import java.util.List; @SuppressWarnings({"all"}) public class SuppressWarnings_ { public static void main(String[] args) { int i = 0; List list = new ArrayList(); list.add("jack"); } } //Type, field, method, parameter, constructor, local of action_ VARIABLE //You can pass in an array /* @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.SOURCE) public @interface SuppressWarnings { String[] value(); } */
Meta annotation
The meta Annotation of JDK is used to modify other annotations to enhance the understanding of source code
Types of meta annotations:
(1) Retention / / specifies the scope of the annotation. There are three types of source, class and runtime
(2) Target / / specifies where annotations can be used
(3) Documented / / specifies whether the annotation will be reflected in the javadoc
(4) Inherited / / the subclass inherits the annotation of the parent class
@Retention annotation
Description can only be used to decorate an Annotation definition to specify how long the Annotation can be retained. @ retention contains a member variable of RetentionPolicy type. When using @ retention, you must specify a value for the value member variable:
@Three values of Retention
(1) RetentionPolicy.SOURCE: after the compiler uses it, it directly discards the comments of this policy
(2) RetentionPolicy.CLASS: the compiler will record annotations in the class file. When running Java programs, the JVM will not retain annotations. This is the default
(3) RetentionPolicy.RUNTIME: the compiler will record the annotation in the class file. When running a Java program, the JVM will retain the annotation. The program can obtain the annotation through reflection
@Documented
It is used to specify that the annotation class modified by meta annotation will be extracted into a document by javadoc tool, that is, the annotation can be seen when generating the document.
The rention that defines a Documented annotation must be set to RUNTIME