todo Java注解
概論
1、注解、注釋annotation
2、是一種引用數據類型。編譯之后也是xxx.class文件
3、怎么自定義注解,語法
[修飾符列表] @interface 注解類型名{
}
4、注解怎么使用
使用時語法格式: @注解類型名
可以出現在:類上、屬性上、方法上、變量上、注解上
5、需要學:Override、Deprecated、Target、Retention
6、元注解:用來標注“注解類型”的注解。例如@Target、@Retention
關于Target注解
這是元注解,用來標注”元注解“
這個注解標注 被標注的注解可以出現在哪些位置
@Target(ElementType.METHOD):表示被標注的注解只能出現在方法
例如:Override 注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
關于Retention注解
這是元注解,用來標注”元注解“
這個注解標注 被標注的注解保存在什么位置上。
例如:@Retention(RetentionPolicy.SOURCE),則表示保存在源文件中
@Retention(RetentionPolicy.CLASS),則表示保存在class文件中
@Retention(RetentionPolicy.RUNTIME),則表示保存在class文件中,并可以被反射機制讀取
@Override
/* 關于Override注解@Override只能注解方法這個注解是給編譯器參考的,和運行階段沒有關系凡是帶有這個注解的,編譯器都會檢查,不是重寫父類方法,編譯器報錯 源代碼:public @interface Override {}*/public class AnnotationTest2 {//@Override 方法寫錯了,這里會有提醒public String tostring() {return "AnnotationTest2{}";} }@Deprecated
//@Deprecated 表示類過時 public class AnnotationTest3 {public static void main(String[] args) {AnnotationTest3.doSome();}//@Deprecated 表示方法過時public static void doSome(){System.out.println("doSome");}public static void doOther(){System.out.println("doOther");} }@Deprecated的源代碼:
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE}) public @interface Deprecated { @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE}) //表示可以出現在構造函數、屬性、局部變量、方法.......元注解
@Target,@Retention
自定義
示例1
public @interface MyAnotation {//稱為注解當中的屬性,不是方法//以下是MyAnotation的name屬性String name();//給屬性默認值int age() default 25; } 使用自定義注解 import com.javacode.classcode.MyAnotation;public class MyAnnotationTest {//直接寫@MyAnotation,會報錯,原因是:如果注解中有屬性,則必須給屬性賦值@MyAnotation(name="zhangsan")//有默認值的可以不寫,屬性名=屬性值的格式public static void doSome(){} }示例2
public @interface MyAnnotation {String value(); }//使用自定義注解2
package com.javacode.annotation2;public class MyAnnotationTest {@MyAnnotation(value="1")public static void doSome(){}//沒有報錯。**因為:如果注解只有一個屬性,且屬性名是value,可以不寫value= 直接寫后面的值@MyAnnotation("1")public static void doOther(){} }自定義注解的屬性
可以是:byte short int long float double boolean char String Class 枚舉,以及以上各項的數組形式
反射注解
//只允許這個注解標注類、方法 @Target(value={ElementType.TYPE,ElementType.METHOD}) //希望注解可以被反射 @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { }使用自定義注解
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; package com.javacode.annotation4;public class MyAnnotationTest {//@MyAnnotation 無法使用在Field上。如果要使用//需要將@Target(value={ElementType.TYPE,ElementType.METHOD})//改為@Target(value={ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})int i;//@MyAnnotation 也無法使用public MyAnnotationTest(int i) {this.i = i;}@MyAnnotation //可以使用public void doSome(){} }反射機制讀取注解
package com.javacode.annotation4;public class ReflectAnnotationTest {public static void main(String[] args) throws Exception{//獲取類Class c=Class.forName("com.javacode.annotation4.MyAnnotationTest");//判讀類上是否有注解//System.out.println(c.isAnnotationPresent(MyAnnotation.class));if(c.isAnnotationPresent(MyAnnotation.class)){MyAnnotation ma=(MyAnnotation) c.getAnnotation(MyAnnotation.class);System.out.println("類上面的注解對象"+ma);//類上面的注解對象@com.javacode.annotation4.MyAnnotation()//獲取注解對象的屬性String s=ma.value();System.out.println(s);}} }獲取方法上的注解
改寫注解
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;//只允許這個注解標注類、方法 @Target(value={ElementType.TYPE,ElementType.METHOD}) //希望注解可以被反射 @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation {String value() default "北京";String username();String password(); }使用改寫注解
//@MyAnnotation public class MyAnnotationTest {//@MyAnnotation 無法使用在Field上。如果要使用//需要將@Target(value={ElementType.TYPE,ElementType.METHOD})//改為@Target(value={ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})int i;//@MyAnnotation 也無法使用public MyAnnotationTest(int i) {this.i = i;}@MyAnnotation(username = "asd",password = "123") //可以使用public void doSome(){}}獲取注解
import java.lang.reflect.Method;public class ReflectAnnotationTest {public static void main(String[] args) throws Exception{//獲取類Class c=Class.forName("com.javacode.annotation4.MyAnnotationTest");//獲取doSomoe()方法Method doSomeMethod=c.getDeclaredMethod("doSome");if(doSomeMethod.isAnnotationPresent(MyAnnotation.class)){MyAnnotation ma=(MyAnnotation) doSomeMethod.getAnnotation(MyAnnotation.class);System.out.println(ma.username());System.out.println(ma.password());}} }實際使用
需求:@ID注解,只能出現在類上面,當類上有這個注解的時候。要求類必須有一個int類型的id值
沒有這個屬性就報異常,有就繼續執行
定義注解
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;//元注解的使用 //表示只能出現在類上 @Target(value= ElementType.TYPE)//ID只能用在類和方法上面 //該注解可以被反射機制讀取 @Retention(RetentionPolicy.RUNTIME) //RUNTIME寫成source說明只保留在Java源文件中不保留在編譯生成的.class文件中,寫的class說明注解被保留到字節碼文件中,RUNTIME說明注解被保留到字節碼文件中并可以被反射機制讀到 public @interface ID { } //這個注解@ID用來標注類,被標注的類必須有一個int類型的ID使用注解的類
@ID public class User {//@ID //int ID;//會報錯,因為ID不能用在屬性String name;}定義異常
public class NotHasIDPRopertyException extends RuntimeException{public NotHasIDPRopertyException() {}public NotHasIDPRopertyException(String message) {super(message);} }測試
import java.lang.reflect.Field;public class Test {public static void main(String[] args) throws Exception{Class userClass=Class.forName("com.javacode.annotation5.User");//判斷類上是否存在ID注解boolean islegal=false;//給個默認標記if(userClass.isAnnotationPresent(ID.class)){//當一個類上面有ID注解的時候,要求類中存在int類型的ID屬性。如果沒有則報異常Field[] fields=userClass.getDeclaredFields();for(Field f:fields){if("ID".equals(f.getName())&&"int".equals(f.getType().getSimpleName())){//表示合法islegal=true;System.out.println("合法");break;}}//判斷是否合法if(!islegal){throw new NotHasIDPRopertyException("被@ID注解標注的類必須有int類型ID屬性");}}} }輸出:
Exception in thread “main” com.javacode.annotation5.NotHasIDPRopertyException: 被@ID注解標注的類必須有int類型ID屬性
at com.javacode.annotation5.Test.main(Test.java:23)
總結:
注解作用:做參考作用,相當于一種標記“有這個注解怎么辦,沒有注解怎么辦”
總結
以上是生活随笔為你收集整理的todo Java注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PID算法的简单了解
- 下一篇: Java根据模板生成PDF文件|添加盖章