當前位置:成語大全網 - 書法字典 - java註釋有哪些?

java註釋有哪些?

Java註釋相當於類、方法或變量的附加註釋(標識)。以下轉載。

-

Annotation是繼Java5和6之後的新特性(中文叫annotation),已經得到了廣泛的應用,比如Spring、Hibernate3、Struts2、iBatis3、JPA、JUnit等等。通過使用註釋,代碼的靈活性大大提高了。

這些是由他人定義的註釋。壹般來說,註釋的使用是在壹些基本框架或者類庫中定義的。所以很少有人會寫壹個註釋,用在程序裏。所以註釋的使用往往給人壹種神秘的感覺,會為妳揭開註釋的面紗。

註釋的奧秘在於它可以控制程序的壹些行為,運行時狀態,給成員賦值,做配置信息等。,與常規的編碼思維大相徑庭。

僅僅用別人定義的註釋是無法理解這些問題的。如果我們真的想知道註釋裏面的秘密,就應該自己定義註釋,然後在程序中獲取註釋信息,可以為我所用。

我來簡單演示壹下類註釋、方法註釋、字段註釋三種類型註釋的用法,看看如何獲取註釋的信息。

壹.定義說明

包lavasoft.anntest

導入Java . lang . annotation . element type;

導入Java . lang . annotation . retention;

導入Java . lang . annotation . retention policy;

導入Java . lang . annotation . target;

/**

*類註釋

*

* @作者雷誌民2009-12-18 14:15:46

*/

@保留(RetentionPolicy。運行時間)

@Target(ElementType。類型)

public @ interface my annotation 4 class {

公共字符串msg();

}

包lavasoft.anntest

導入Java . lang . annotation . element type;

導入Java . lang . annotation . retention;

導入Java . lang . annotation . retention policy;

導入Java . lang . annotation . target;

/**

*方法說明

*

* @作者雷誌民2009-12-18 14:16:05

*/

@保留(RetentionPolicy。運行時間)

@Target(ElementType。方法)

public @ interface my annotation 4 method {

公共字符串msg 1();

公共字符串msg 2();

}

包lavasoft.anntest

導入Java . lang . annotation . element type;

導入Java . lang . annotation . retention;

導入Java . lang . annotation . retention policy;

導入Java . lang . annotation . target;

/**

*現場評論

*

* @作者雷誌民2009-12-18 15:23:12

*/

@保留(RetentionPolicy。運行時間)

@Target(ElementType。場)

public @ interface my annotation 4 field {

公共字符串commont();

公共布爾請求();

}

第二,寫壹個類,使用這些註釋。

包lavasoft.anntest

/**

*壹個普通的Java類

*/

@MyAnnotation4Class(msg = "測試類註釋信息")

類別測試類別{

@ myannotation4field (common = "成員變量的註釋信息",request = true)

私有字符串測試字段;

@ my annotation 4 method(msg 1 = "測試方法註釋信息1 ",msg2 = "測試方法註釋信息2 ")

公共void testMethod() {

System.out.println("Hello World!");

}

}

三、考試筆記

為了使用註釋,您需要通過反射獲得帶註釋的對象。通過註釋對象操作註釋信息。

包lavasoft.anntest

導入Java . lang . reflect . field;

導入Java . lang . reflect . method;

/**

*測試類別

*

* @作者雷誌民2009-12-18 14:13:02

*/

公共類TestOptAnnotation {

公共靜態void main(String[] args)引發NoSuchMethodException,NoSuchFieldException {

test class t = new test class();

系統。out.println (" - My Annotation 4類註釋信息);

my annotation 4 class an 4 clazz = t . get class()。get annotation(my annotation 4 class . class);

system . out . println(an 4 clazz . msg());

系統。out.println (" - my annotation 4方法註釋信息);

方法method = t.getClass()。getMethod("testMethod ",新類[0]);

my annotation 4 method an 4 method = method . get annotation(my annotation 4 method . class);

system . out . println(an 4 method . msg 1());

system . out . println(an 4 method . msg 2());

系統。out.println (" - My Annotation 4字段註釋信息);

Field field = t.getClass()。getDeclaredField(" testfield ");

my annotation 4 field an 4 field = field . get annotation(my annotation 4 field . class);

system . out . println(an 4 field . commont());

system . out . println(an 4 field . request());

}

}

運行結果:

- MyAnnotation4Class 4批註信息。

測試類註釋信息

-我的註釋4方法註釋信息。

測試方法註釋信息1

測試方法註釋信息2

-我的註釋4字段註釋信息。

成員變量的註釋信息

真實的

進程結束,退出代碼為0

第四,總結

看了上面的測試過程,標註就不再神秘了。以下是自定義註釋的使用摘要:

1.自定義標註,註意標註的時空範圍。簡單來說,就是註釋的目標(類、方法和字段),註釋的時間限制(運行時或源代碼中有效)。

2.要獲得註釋信息,必須通過Java反射技術獲得註釋對象,因為妳沒有其他方法獲得註釋對象。

3.獲取註釋對象後,可以調用註釋方法獲取相應的值。由基本框架使用。

4.當然,註釋也可以沒有已定義的成員,因此註釋成為壹個標記符號。