[java notes] annotation

Annotation: Annotation. New technologies introduced by JDK5.0

The role of annotations:

①Not the program itself, you can explain the program

②Can be read by other programs

Annotation format: @Annotation name. In addition, you can add some parameter values

//例
@SuppressWarnings(value="unchecked")

Where can annotations be used?

It can be attached to package, class, method, filed, etc., which is equivalent to adding additional auxiliary information to them, and can programmatically access these metadata through reflection mechanism


Built-in annotations:

@Override Defined in java.lang.Override, it is used to decorate methods, indicating that a method declaration intends to override another method declaration of the superclass
@Deprecated Decorate a method, property, class to indicate that programmers are discouraged from using such an element, usually because it is dangerous or a better alternative exists
@SuppressWarnings Used to suppress compile-time warning messages, you need to add a parameter to use

@Override

 

 @Deprecated

 @SuppressWarnings

If you define an element that is not used, a Variable is never used warning will appear

Add @SuppressWarnings("all") before the method to suppress the warning message

@SuppressWarnings can also be added in front of the class 

 

 


Meta- annotation : meta-annotation type, responsible for annotating other annotations, used to describe other annotation types

@Target

Used to describe the scope of use of the annotation

@Retention

Declaration cycle used to describe annotations

SOURCE<CLASS<RUNTIME

@Document Indicates that the annotation is included in the javadoc
@Inherited Indicates that subclasses can inherit the annotation from the parent class

 Define a meta-annotation:

@Target: Indicates where the annotation can be used

 

 @Retention: Define the period of the annotation, generally set to RUNTIME to indicate that it is valid at runtime

 @Document indicates whether to generate annotations in JAVAdoc

@Inherited subclasses can inherit the annotations of the parent class 


Custom annotations:

@interface custom annotation, which automatically inherits the java.lang.annotation.Annotation interface

 Parameters with default values:

 

 

 

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124075973