休息一下,鍛鍊了一下身體繼續
我們也可以定義自己的Annotation
package cn.yangtao.ceshi;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS) ---------------------------->定義該註解的有效區域,還有SOURCE、RUNTIME
public @interface MyAnnotationDemo01 {
String key();----------------------------------------------->用來接收傳遞進去的值
String value();
}
使用MyAnnotationDemo01
package cn.yangtao.ceshi;
public class StudentDemo {
@SuppressWarnings("deprecation")
@MyAnnotationDemo01(key="hello",value="hello world") 這行是最關鍵的,記住賦值的方式
public static void main(String args[]){
new StudentDemo().sayHello();
}
@ Deprecated
public void sayHello(){
System.out.println("已經覆寫");
}
}
對於賦值可以有一下幾種情況
1、數組型
package cn.yangtao.ceshi;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
String[] key();
}
賦值方式
package cn.yangtao.ceshi;
public class StudentDemo {
@SuppressWarnings("deprecation")
@MyAnnotationDemo01(key={"hello","world"})
public static void main(String args[]){
new StudentDemo().sayHello();
}
@ Deprecated
public void sayHello(){
System.out.println("已經覆寫");
}
}
2、設置默認型
package cn.yangtao.ceshi;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
String key() default "hello";
}
如果你使用該註解時沒有給註解導入屬性,則就會默認使用"hello"
3、可以用枚舉來限制取值範圍
定義枚舉類
package cn.yangtao.ceshi;
public enum Color {
Green,Red,Blue;
}
標明只能給註解傳入枚舉屬性
package cn.yangtao.ceshi;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
Color key() ;
}
使用註解
package cn.yangtao.ceshi;
public class StudentDemo {
@SuppressWarnings("deprecation")
@MyAnnotationDemo01(key=Color.Blue) 這個時候就只能取枚舉中的對象,如果你有很高的控制慾這一招很好使
public static void main(String args[]){
new StudentDemo().sayHello();
}
@ Deprecated
public void sayHello(){
System.out.println("已經覆寫");
}
}
能夠設置屬性了,要怎麼拿到和使用屬性呢
?
答案:反射找到註解所在的方法(如果忘記去看反射)------------>找到該方法上的註解
枚舉
package cn.yangtao.ceshi;
public enum Color {
Green,Red,Blue;
}
註解
package cn.yangtao.ceshi;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) 這個地方要設成運行時,否則在取屬性的時候會出現空指向異常
public @interface MyAnnotationDemo01 {
Color key() ;
}
取出useAnnotation方法上註解的屬性
package cn.yangtao.ceshi;
public class StudentDemo {
@SuppressWarnings("deprecation")
public static void main(String args[]) throws SecurityException, NoSuchMethodException{
useAnnotation();
MyAnnotationDemo01 mt=StudentDemo.class.getMethod("useAnnotation", null).
getAnnotation(MyAnnotationDemo01.class);
Color c=mt.key(); 取出屬性的值
System.out.println(c.name()); 獲得枚舉的對象的姓名
}
@MyAnnotationDemo01(key=Color.Blue) 設置該屬性的內容
public static void useAnnotation() {
// TODO Auto-generated method stub
}
}
枚舉中元註解
概念:註解的註解就是元註解
常用的有:
@Target 聲明被他標註的註解的作用區域比如 類、方法、屬性、包等
@Documented 目前不是很清楚
@Inherited 聲明該註解可以被繼承