第九章:注解
注解
1. 注解,或者叫做注释类型,英文单词是Annotation
注解是干什么的???
2. 注解是一种应用数据类型。编译之后也是生成xxx.class文件
3. 怎么自定义注解??语法格式是什么??
[修饰符列表] @interface 注解类型名{
}
4. 注解怎么使用,用在什么地方
第一:注解使用时的语法格式是:
@注解类型名
第二:注解可以出现在类上、属性上、方法上、变量上、等...
注解还可以出现在注解类型上。
5.JDK内置了哪些注解??
java.lang包下的注释类型
Deprecated 用@Deprecated 注释的程序元素(表示这个东西已过时),不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择。
Override 表示一个方法声明打算重写超类中的另一个方法声明。
SuppressWarnings 指示应该在注释元素(以及包含在该注释元素中的所有程序元素)中取消显示指定的编译器警告。
6. 元注解
什么是元注解??
用来标注“注解类型”的“注解”,称为元注解
常见的元注解有哪些??
Target
Retention
关于Target注解:
这是一个元注解,用来标注“注解类型”的“注解”
这个Target用来标注“被标注的注解”可以出现在哪些位置上。
@Target(ElementType.METHOD):表示“被标注的注解”只能出现在方法上
关于Retention注解:
这是一个元注解,用来标注“注解类型”的“注解”
这个Retention用来标注“被标注的注解”最终保存在哪里。
@Retention(RetentionPolicy.SOURCE):表示该注解值被保留在java源文件中。
@Retention(RetentionPolicy.CLASS):表示该注解被保留在class文件中
@Retention(RetentionPolicy.RUNTIME):表示该注解被保存在class文件中,并且可以被反射机制读取到。
@MyAnnotation
public class AnnotationTest01 {
@MyAnnotation
private int no;
@MyAnnotation
public AnnotationTest01() {
}
@MyAnnotation
public static void m1(){
@MyAnnotation
int i =100;
}
public void m2(){
}
}
@MyAnnotation
interface MyInterface{
}
@MyAnnotation
enum Season{
SPRING,SUMMER,AUTUMN,WINTER
}
public class AnnotationTest02 {
@Override
public String toString() {
return "toString";
}
}
public class AnnotationTest03 {
@Deprecated
public static void doSome(){
System.out.println("do something");
}
@Deprecated
public static void doOther(){
System.out.println("do other");
}
}
class T{
public static void main(String[] args) {
AnnotationTest03.doSome();
}
}
public class MyAnnotationTest {
@MyAnnotation(name = "zhangsan",color = "red")
public void doSome(){
}
}
public class ReflectAnnotationTest {
public static void main(String[] args) throws Exception{
Class c = Class.forName("进阶.注解09.Annotation2.MyAnnotationTest");
System.out.println(c.isAnnotationPresent(MyAnnotation.class));
if (c.isAnnotationPresent(MyAnnotation.class)){
MyAnnotation myAnnotation = (MyAnnotation)c.getAnnotation(MyAnnotation.class);
System.out.println("类上面的注解对象:"+ myAnnotation);
System.out.println(myAnnotation.value());
}
Class stingClass = String.class;
System.out.println(stingClass.isAnnotationPresent(MyAnnotation.class));
}
}
@MyAnnotation
public class MyAnnotationTest {
int i;
public MyAnnotationTest() {
}
@MyAnnotation
public void doSome(){
int i;
}
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "String";
}
public class MyAnnotationTest {
@MyAnnotation(username = "admin",password = "123")
public void doSome(){
}
public static void main(String[] args) throws Exception{
Class c = Class.forName("进阶.注解09.annotation3.MyAnnotationTest");
Method doSomeMethod = c.getDeclaredMethod("doSome");
if (doSomeMethod.isAnnotationPresent(MyAnnotation.class)){
MyAnnotation myAnnotation = doSomeMethod.getAnnotation(MyAnnotation.class);
System.out.println(myAnnotation.username());
System.out.println(myAnnotation.password());
}
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
String username();
String password();
}