【Java】-NO.17.EBook.4.Java.1.014-【疯狂Java讲义第3版 李刚】- Annotation

 1.0.0 Summary

Tittle:【Java】-NO.17.EBook.4.Java.1.014-【疯狂Java讲义第3版 李刚】-  Annotation

Style:EBook

Series:Java

Since:2017-11-01

End:....

Total Hours:...

Degree Of Diffculty:2

Degree Of Mastery:2

Practical Level:2

Desired Goal:2

Archieve Goal:....

Gerneral Evaluation:...

Writer:kingdelee

Related Links:

http://www.cnblogs.com/kingdelee/

1.java.lang 的 5个Annotation

api :https://docs.oracle.com/javase/9/docs/api/java/lang/package-summary.html

Annotation Types Summary
Annotation Type Description
Deprecated
A program element annotated  @Deprecated is one that programmers are discouraged from using.
FunctionalInterface
An informative annotation type used to indicate that an interface type declaration is intended to be a  functional interface as defined by the Java Language Specification.
Override
Indicates that a method declaration is intended to override a method declaration in a supertype.
SafeVarargs
A programmer assertion that the body of the annotated method or constructor does not perform potentially unsafe operations on its varargs parameter.
SuppressWarnings
Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element).

2.java.lang.annotation 的 6个meta Annotation

https://docs.oracle.com/javase/9/docs/api/java/lang/annotation/package-summary.html

Annotation Types Summary
Annotation Type Description
Documented
If the annotation  @Documented is present on the declaration of an annotation type  A, then any  @A annotation on an element is considered part of the element's public contract.
Inherited
Indicates that an annotation type is automatically inherited.
Native
Indicates that a field defining a constant value may be referenced from native code.
Repeatable
The annotation type  java.lang.annotation.Repeatable is used to indicate that the annotation type whose declaration it (meta-)annotates is  repeatable.
Retention
Indicates how long annotations with the annotated type are to be retained.
Target
Indicates the contexts in which an annotation type is applicable.

2.1 @Retention

当需要使用的注解考虑在运行时可以被程序反射读取,则在该注解上加 

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation{}

  

2.2 @Target

 用于指定注解修饰的作用域

// 我定义的注解只能修饰成员变量
@Target(ElementType.FIELD)
public @interface MyAnnotation{}

  

2.3 @Document

指定可以被提取出文档,一般需要出现在文档的都是必写。

 2.4 @Inherited

类B继承类A,类A使用@Inherited时,类B同样继承了类A的@Inherited特性

2.5 @Repeatable

3. 反射获取注解信息

猜你喜欢

转载自www.cnblogs.com/kingdelee/p/7768120.html
今日推荐