Java复习笔记:注解
文章发布时间:
最后更新时间:
最后更新时间:
Java 内置注解
@Override
定义在:java.lang.Override 中
只能用于修辞方法,表示一个方法声明打算重写超类中的另一个方法。
@Deprecated
定义在:java.lang.Deprecated 中
可以用于修辞方法、属性、类,表示不推荐程序员使用的元素,通常意味着它存在危险或有更好的选择
@SuppressWarnings
定义在:java.lang.SuppressWarnings 中
用于抑制编译时的警告信息
@SuppressWarnings("all"): 抑制所有类型的警告@SuppressWarnings(value={"unchecked", "rawtypes"}): 抑制所多种类型的警告@SuppressWarnings("unchecked"): 抑制unchecked类型的警告
参数:
all: to suppress all warningsboxing: to suppress warnings relative to boxing/unboxing operationscast: to suppress warnings relative to cast operationsdep-ann: to suppress warnings relative to deprecated annotationdeprecation: to suppress warnings relative to deprecationfallthrough: to suppress warnings relative to missing breaks in switch statementsfinally: to suppress warnings relative to finally block that don’t returnhiding: to suppress warnings relative to locals that hide variableincomplete-switch: to suppress warnings relative to missing entries in a switch statement (enum case)nls: to suppress warnings relative to non-nls string literalsnull: to suppress warnings relative to null analysisrawtypes: to suppress warnings relative to un-specific types when using generics on class paramsrestriction: to suppress warnings relative to usage of discouraged or forbidden referencesserial: to suppress warnings relative to missing serialVersionUID field for a serializable classstatic-access: o suppress warnings relative to incorrect static accesssynthetic-access: to suppress warnings relative to unoptimized access from inner classesunchecked: to suppress warnings relative to unchecked operationsunqualified-field-access: to suppress warnings relative to field access unqualifiedunused: to suppress warnings relative to unused code
元注解(meta-annotation)
用于注解其它注解的注解
1. @Target
用于描述注解的使用范围
example:
1 | |
可用{}来给value传入数组
1 | |
value参数值为枚举类型,定义如下:
1 | |
2. @Retention
表示需要什么级别保存该注解信息,用于描述注解的生命周期
取值:SOURCE < CLASS < RUNTIME
SOURCE: 保留到源码CLASS: 保留到字节码RUNTIME: 保留到虚拟机
1 | |
3. @Document
表示该注解将被包含在javadoc中
4. @Inherited
表示子类可以继承父类中的该注解
自定义注解
反射(Reflection)
反射(Reflection)是Java被视为动态语言的关键,反射机制允许程序在执行期借助于Reflection API取得任何类型的内部信息,并能直接操作任意对象的内部属性及方法。
加载完类之后,在堆内存的方法区中就产生了一个Class类型的对象(一个类只有一个Class对象),这个对象就包含了完整的类的结构信息。我们可以通过这个对象看到类的结构。