一云矫、廢話
上一次寫的簡書,現(xiàn)在看汗菜,很尷尬(?.?)我很想刪掉让禀,但是畢竟第一次還是不刪了吧挑社。今天想學(xué)習(xí)一下Java中的注解,就當(dāng)記錄一下(順便學(xué)習(xí)使用一下Markdown)巡揍。
二痛阻、發(fā)現(xiàn)與認(rèn)識
public class IncomeFragment extends BaseFragment {
@Bind(R.id.iv_user_img)
ImageView mIvUserImg;
@Bind(R.id.tv_income_user_name)
TextView mTvIncomeUserName;
@Bind(R.id.tv_total_income)
TextView mTvTotalIncome;
@Bind(R.id.tv_record)
TextView mTvRecord;
@Bind(R.id.tv_bank_count)
TextView mTvBankCount;
@Override
protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_income, container, false);
- 代碼上@Override(重寫方法的注解),其實(shí)這是JDK1.5以后引入的新特性
JDK1.5之后內(nèi)部提供的三個(gè)注解
@Deprecated 意思是“廢棄的腮敌,過時(shí)的”
@Override 意思是“重寫阱当、覆蓋”
@SuppressWarnings 意思是“壓縮警告” - 用著Butterknife開發(fā)的安卓童鞋肯定知道一鍵初始化控件帶來方便
看一下@Bind的源碼
/*****
*****Bind a field to the view for the specified ID. The view will automatically be cast to the field
*****type.
*****{@literal @}Bind(R.id.title) TextView title;
*****/
@Retention(CLASS)
@Target(FIELD)
public @interface Bind {
/** View ID to which the field will be bound. */
int[] value();
}
從源碼上看注解類似于接口(其實(shí)就是一種特殊接口)
注解(Annotation)相當(dāng)于一種標(biāo)記糜工,javac編譯器、開發(fā)工具和其他程序可以通過反射機(jī)制來了解你的類及各種屬性和方法上有無某個(gè)標(biāo)記捌木,就去干相應(yīng)的事,標(biāo)記可以加在包刨裆、類澈圈,屬性、方法帆啃,方法的參數(shù)以及局部變量上瞬女。
三、了解與使用
認(rèn)識注解前先認(rèn)識元注解链瓦,元注解:在注解類上使用另一個(gè)注解類拆魏,那么被使用的注解類就稱為元注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/*****
*****創(chuàng)建者: LeeBoo
*****創(chuàng)建時(shí)間:2016/12/29 10:44
*****描述: 自定義注解 Annotation
*****/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Annotation {
}
上面代碼的Retention和Target稱為元注解
- @Retention(RetentionPolicy.RUNTIME)
Retention注解決定Annotation注解的生命周期 - @Target({ElementType.METHOD, ElementType.TYPE})
Target注解決定Annotation注解可以加在類身上,或者屬性身上慈俯,或者方法等成分身上
Retention元注解有3種value屬性值
@Retention(RetentionPolicy.SOURCE)
Annotation注解生命周期:只在java源文件中存在渤刃,javac編譯成.class文件后注解就不存在了
@Retention(RetentionPolicy.CLASS)
Annotation注解生命周期:在java源文件(.java文件)中存在,編譯成.class文件后注解也還存在贴膘,被Annotation注解類標(biāo)識的類被類加載器加載到內(nèi)存中后Annotation注解就不存在了
@Retention(RetentionPolicy.RUNTIME)
Annotation注解生命周期:讓Annotation這個(gè)注解的生命周期一直程序運(yùn)行時(shí)都存在
@Target元注解決定了一個(gè)注解可以標(biāo)識到哪里卖子,如標(biāo)識在類上,在屬性上刑峡,或者在方法上and so on洋闽, @Target默認(rèn)值為可標(biāo)識在任何地方
此時(shí)不是該看看@Deprecated、@Override突梦、@SuppressWarnings這三個(gè)注解的@Retention和@Target注解的屬性值分別是什么嗎诫舅??宫患?
3.1為注解增加屬性
@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.METHOD, ElementType.TYPE })
public @interface Annotation {
boolean isProgrammer ();//添加屬性
**注解的屬性定義方式就和接口中定義方法的方式一樣**
}
3.1.1為注解增加屬性的默認(rèn)值
@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.METHOD, ElementType.TYPE })
public @interface Annotation {
boolean isProgrammer() default true;//添加屬性默認(rèn)值true
}
3.1.2注解中有一個(gè)名稱為value的屬性
@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.METHOD, ElementType.TYPE })
public @interface Annotation {
boolean isProgrammer() default true;//添加屬性默認(rèn)值true
String value() default "Java Programmer"; //名稱為value的屬性 默認(rèn)值為"Java Programmer"
}
3.2使用注解屬性
//應(yīng)用Annotation注解的isProgrammer屬性為true
寫法1
@Annotation(isProgrammer = true)
寫法2
@Annotation
//應(yīng)用Annotation注解的value屬性為Java Programmer
寫法1
@Annotation(value = "Java Programmer")
寫法2
@Annotation("Java Programmer") **即value=可以省略**
public class AnnotationUse {
}
3.3注解高級屬性
3.3.1刊懈、數(shù)組類型的屬性
注解類添加數(shù)組類型的屬性:int[] id() default {1,2,3};
使用類使用數(shù)組類型的屬性:@Annotation(id={2,3,4})
如果數(shù)組屬性只有一個(gè)值,這時(shí)候?qū)傩灾挡糠挚梢允÷源罄ㄌ枺纾篅Annotation(id=2)虚汛,表示數(shù)組屬性只有一個(gè)值匾浪,值為2(如同Butterknife中onClick注解)
3.3.2、枚舉類型的屬性
注解類添加枚舉類型的屬性:Language language() default Language.OC;
使用類使用枚舉類型的屬性:@Annotation(language=Language.JAVA)
3.3.3卷哩、元注解類型的屬性
a蛋辈、元注解類型的屬性創(chuàng)建
public @interface GradeAnnotation {
String grade() default "高級開發(fā)程序員";//元注解GradeAnnotation設(shè)置有一個(gè)屬性grade
}
b、元注解類型的屬性添加
- Language 枚舉
public enum Language {
OC, //object-C
JAVA //java
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Annotation {
//添加一個(gè)int類型數(shù)組的屬性
int[] id() default {1,2,4};
//添加一個(gè)枚舉類型的屬性将谊,并指定枚舉屬性的缺省值冷溶,缺省值只能從枚舉類Language 中定義的枚舉對象中取出任意一個(gè)作為缺省值
Language language() default Language.OC;
//**為注解添加一個(gè)注解類型的屬性,并指定注解屬性的缺省值**
GradeAnnotation gradeAnnotation () default @GradeAnnotation(grade = "初級開發(fā)程序員");
}
c、元注解類型的屬性使用
@Annotation( id = {8,0,8}, language = Language.JAVA, gradeAnnotation = @GradeAnnotation (grade = "中級開發(fā)程序員"))
public class AnnotationUse {
@Annotation //將Annotation注解標(biāo)注到main方法上
public static void main(String[] args) {
**使用反射機(jī)制對Annotation類的檢查**
一旦在某個(gè)類上使用了@Annotation尊浓,那么這個(gè)Annotation類的實(shí)例對象annotation就會被創(chuàng)建出來了
Annotation annotation = (Annotation) AnnotationTest.class.getAnnotation(Annotation.class);
System.out.println(annotation.language());//JAVA
System.out.println(annotation.id().length);//3
GradeAnnotation ga = annotation.gradeAnnotation();
System.out.println(ga.grade());//輸出的結(jié)果為:"中級開發(fā)程序員"
}
}