文档介绍:第18章给编译器看的注释——Annotation
Annotation
系统内建的Annotation
自定义Annotation
Rentention和RententionPolicy
反射与Annotation
深入Annotation
Annotation
Annotation实际上表示的是一种注释的语法,在Java中最早的程序是提倡程序与配置代码相分离,而最新的理论是将所有的配置直接写入到程序之中,那么如果要想完成这样的功能,则就要使用Annotation
系统内建的Annotation
***@Override
***@Deprecated
***@SuppressWarnings
***@Override
在覆写的时候可以明确的使用***@Override表示方法是属于覆写的操作
.;
public class Studnt extends Person {
***@Override
public String say(){
return "学生在说话。" ;
}
}
***@Deprecated
***@Deprecated注释表示是不建议使用的操作
.;
public class Info {
***@Deprecated
public String getInfo(){
return "hello" ;
}
}
***@SuppressWarnings
***@SuppressWarings表示的是压制警告,如果有一些警告信息,则可以压制掉,不出现警告的提示
.;
public class TestInfo {
***@SuppressWarnings("deprecation")
public static void main(String[] args) {
new Info().getInfo() ;
}
}
自定义Annotation
定义Annotation的语法
public ***@interface Annotation的名称{}
定义一个简单的Annotation
.;
public ***@interface MyAnnotation {
}
Rentention和RententionPolicy
RetentionPolicy中规定了以下的三个范围
⑴只在源代码中起作用:public static final
RetentionPolicy SOURCE
⑵只在编译之后的class中起作用:public static
final RetentionPolicy CLASS
⑶在运行的时候起作用:public static final
RetentionPolicy RUNTIME
反射与Annotation
取得全部的Annotation
加入自定义的Annotation
取得全部的Annotation
在一个方法上使用三个内建的Annotation声明
.;
public class Info {
***@Override
***@Deprecated
***@SuppressWarnings(value="")
public String toString() {
return "hello" ;
}
}