The difference between Java annotations and annotations

Notes is easy to know is to make yourself, or someone else in your code easy to read

The role of annotations

1) is not a program in itself, can explain the program. (This is no different with comment)
2) may be another program ( for example : compilers, etc. ) is read. ( Notes information processing flow is a major difference between notes and comments, if there is no annotation information processing flow, the annotation meaningless)
Notes format:
1) notes is "@ Notes name " exists in the code, you can also add some parameters
Value, e.g. @SuppressWarnings (value = "an unchecked") .
Where the use of annotations
1) can be attached to the package, class, method, field above and so on, which is equivalent to adding additional auxiliary information, we can enable access to these elements by programming reflection.
Built-in notes
1) @Override: identification method is a method of overwriting
2) @Deprecated: method of identification is not recommended
3) @SuppressWarnings : used to suppress warning messages when compiling @SuppressWarinings need to provide parameters to normal use, these parameters are already defined, we only need to select it.
Custom annotation:
6.1 custom annotation syntax
Use @interface custom annotations automatically inherits the interface definition java.lang.annotation.Annotation
1) @interface used to declare a comment
2) each of which actually is a method declares a configuration parameter
a) name of the method is the name of the parameter
b) return type is the type of the parameter ( return value type can only be substantially
Type, Class , String , enum)
c) by default to declare the default value of the parameter
d) If only one member of the general parameter called value
Note: annotation element must have values. When we define annotation elements, the
Often empty string, 0 as a default value. Often use a negative ( such as -1) represent the meanings do not exist
6.2 meta-annotation
Meta-annotation role is responsible for other annotation notes. In Java defined in four standard meta-annotation type, which are used to provide other types of these annotation types and stated that they support java.lang.annotation classes can be found in the package
1) @Target
2) @Retention
3) @Documented
4) @Inherited
6.3 @Target
Action: is used to describe use of annotations ( i.e. annotations described may be used in any place)
For example :
@Target(value=ElementType.TYPE) 6.4 @Retention
Role: expressed the need to save the annotation information on what level , is used to describe annotation
Life cycle
Published 178 original articles · won praise 14 · views 20000 +

Guess you like

Origin blog.csdn.net/ZGL_cyy/article/details/104406305