Java-???????????
???????????? ???????[ 2014/3/27 11:03:27 ] ????????Java ???
????RetentionPolicy ??????????????????????SOURCE??CLASS ?? RUNTIME ???????Java??????class?????????е??????
????????????????????????AnimTest ???????Retention???????????CLASS????????????????????????м???????????????????????????????????AnimTest?????????????????????????????????????????????????????????????????????
/**
* ??????????
* @author Administrator
*
*/
@Retention(value = RetentionPolicy.RUNTIME) //???????????????????
public @interface AnimTest {
}
????????д?????????it has one
????3???????и???????????????÷?Χ???????????????????????
????????и?????Tagert???????Χ???Tagert ?????????????????????????????????????????????Tagert?????????????????κ????????????????????????????????????Χ
?????????AnimTest??????????????????????????Tagert
/**
* ??????????
* @author Administrator
*
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD}) //?????Χ???????????
public @interface AnimTest {
}
????????????????????????????????????????????????????????????????£?
public class ZhujieTest {
/**
* @param args
*/
@AnimTest //???????????
public static void main(String[] args) {
boolean isAnim = ZhujieTest.class.isAnnotationPresent(AnimTest.class);
if(isAnim)
System.out.println("it has one");
else
System.out.println("no have");
}
}
???????????????????????????????? ?????????????? @Target(value = {ElementType.METHOD??ElementType.TYPE})??type????????
???????????????????????
????1?????????????????????????????????????????
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.itcast.classdemo.WeekDay;
/**
* ??????????
* @author Administrator
*
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD??ElementType.TYPE??ElementType.ANNOTATION_TYPE})
public @interface AnimTest {
String color(); //?????
int count(); //????
int[] arr(); //????
MetaAnnotation meta() default @MetaAnnotation("very food"); //???????
WeekDay day() default WeekDay.MON; //???????
}
//?????
public @interface MetaAnnotation {
String value() default "heollo"; //??????
}
//?????
public enum WeekDay {
SUN??MON
}
????2???????????????????????
import com.itcast.zhujie.AnimTest;
@AnimTest (color ="red"??count =1??arr=234) //????????????????????????
public class ZhujieTest {
/**
* @param args
*/
@AnimTest (color ="blue"??count=2??arr={3??21})
public static void main(String[] args) {
//?????AnimTest?????
boolean isAnim = ZhujieTest.class.isAnnotationPresent(AnimTest.class);
if(isAnim){
//???AnimTest????????
AnimTest an = ZhujieTest.class.getAnnotation(AnimTest.class);
//?????????AnimTest?????????????
System.out.println(an.color()); // red
System.out.println(an.count()); //1
System.out.println(Arrays.toString(an.arr())); //[234]
System.out.println(an.meta().value()); //very good
System.out.println(an.day()); //MON
System.out.println("it has one");
}else
System.out.println("no have");
}
}
?????????????????
????1?????????????????String??int?????顢???????
????2???????????????????????????String color() default "red";
????3???????????????????????????????????????????????????? ?? @AnimTest (color ="blue"??count=2??arr={3??21})
????4?????????????????÷??似??AnimTest an = ZhujieTest.class.getAnnotation(AnimTest.class);
????5??????????????????????÷??????????????????
????6???????????????????????????????????value??????????????????????????磺@MetaAnnotation("very food")
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11