--------------------ASP.Net+Android+IOS開發、.Net培訓、期待與您交流! --------------------
1. 註解
1.概述
註解可以定義到方法上,類上,一個註解相當與一個類,就相當於實例了一個對象,加上了註解,就相當於加了一個標誌。
常用的註解:
@Override:表示重新父類的方法,
這個也可以判斷是否覆蓋的父類方法,在方法前面加上此語句,如果提示的錯誤,那麼你不是覆蓋的父類的方法,要是提示的沒有錯誤,那麼就是覆蓋的父類的方法。
@SuppressWarnings(“deprecation”):取消編譯器的警告(例如你使用的方法過時了)
@Deprecated:在方法的最上邊也上此語句,表示此方法過時,了,或者使用在類上面
例子:就是取消警告
import java.util.ArrayList;
import java.util.List;
public class annotationDemo {
/*
* 對於集合,如果沒有指定存儲的類型,那麼就會有安全警告,
* 如果不想提示安全警告的話,那麼就所在類或者方法上添加@SuppressWarnings(參數)
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List list=new ArrayList();
}
}
2.自定義註解
1.格式
權限 @interface 註解名稱 { }
步驟:
定義註解類--->定義應用註解類的類--->對應用註解類的類進行反射的類(這個類可以另外定義,也可以是在應用註解類中進行測試)
import java.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
//定義此註解保留在字節碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
@MyAnnotation
// 應用定義的註解類
public class ApplyMyAnnotation {
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的註解類
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}
結果:
@www.fuxi.jiaqiang.MyAnnotation()
2.聲明週期
格式:例如:@Retention(RetentionPolicy.CLASS)
在自定一的註解類上定義週期,@Retention(參數類型) 參數類型是RetentionPolicy
RetentionPolicy.CLASS:類文件上,運行時虛擬機不保留註解
RetentionPolicy.RUNTIME:類文件上,運行時虛擬就保留註解
RetentionPolicy.SOURCE:源文件上,丟棄註解
SuppressWarnings和Override是RetentionPolicy.SOURCE,
Deprecated是在RetentionPolicy.RUNTIME,要向運行時調用定義的一樣,那麼必須是RetentionPolicy.RUNTIME,
默認的都是RetentionPolicy.CLASS:
3.指定目標
格式:例如:方法上@Target(ElementType.METHOD)
定義的註解可以註解什麼成員。如果不聲明此註解,那麼就是可以放到任何程序的元素上。
可以是包,接口,參數,方法,局部變量,字段…等。
//定義此註解保留在字節碼中
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})//可以定義在方法上和類上接口,表示類型
public @interface MyAnnotation {
}
@MyAnnotation
// 應用定義的註解類
public class ApplyMyAnnotation {
@MyAnnotation//定義在方法上
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的註解類
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}
3.爲註解添加屬性
1.類型
註解的屬性置可以是:8個基本數據類型,String,枚舉,註解,Class,數組類型,
2.注意點
當注 解中只有一個屬性或者是隻有一個屬性需要賦值的話,那麼在調用的時候,就可以直接寫入,不需要指定屬性名,
當註解的屬性是數組類型並且賦值的時候只賦值一個值,那麼就可以省略{}。
3.示例
3.1.屬性類型(是String)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//定義此註解保留在字節碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//設置默認值是"red"
}
@MyAnnotation("java")
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* 這是獲得類上的註解,也可以獲得方法上的註解,下面就以獲得類上的註解爲例
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的註解類
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
}
}
}
結果:
value=java
Color=red
從調用的程序中,也可以看出,只有一個屬性可以需要賦值的話,可以省略屬性名。否則@註解類(屬性名=值)
3.2.綜合類型
/*枚舉類*/
public enum Week{
SUN,MON;
}
/**
* 註解類
*/
public @interface annotationText {
String value();
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//定義此註解保留在字節碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//設置默認值是"red"
Week week() default Week.MON;//枚舉類型
int [] array() default {1,2,3};//數組類型
annotationText annotation() default @annotationText("MY");//註解類型
Class classDemo() default Integer.class;//Class類型
}
@MyAnnotation(value="java",Color="green",week=Week.SUN,array=5,annotation=@annotationText("YOU"),classDemo=String.class)//數組array={4,5,6}
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* 這是獲得類上的註解,也可以獲得方法上的註解,下面就以獲得類上的註解爲例
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的註解類
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
System.out.println("week="+annotation.week());
System.out.println("array長度="+annotation.array().length);
System.out.println("註解類型值="+annotation.annotation().value());
System.out.println("Class類型值="+annotation.classDemo());
}
}
}
結果:
value=java
Color=green
week=SUN
array長度=1
註解類型值=YOU
Class類型值=classjava.lang.String
4.Method上的註解
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* 註解類
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface annotationText {
String value();
}
public class ApplyMyAnnotation {
public static void main(String[] args) throws Exception {
Method methodshow = ApplyMyAnnotation.class.getMethod("show");
annotationText anno = methodshow.getAnnotation(annotationText.class);
System.out.println(anno.value());
}
@annotationText("java")
public void show() {
System.out.println("hello");
}
}
結果:
java
--------------------ASP.Net+Android+IOS開發、.Net培訓、期待與您交流! --------------------