自定义Annotation类型:标识符(marker Annotation):@interface
Annotation的使用:可以放在任何地方,如类的前面,方法的前面,变量、属性的前面等等。
Annotation的定义:
在定义Annotation型态的时候不能继承其它的Annotation型态或接口
1、不带参数的定义与使用
java 代码
- package com.test;
- public @interface AnnotationTest {
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
2、带一个参数的定义与使用,请注意:
如果定义的参数是"value"这个字符串,那么在使用时可以这样写:@AnnotationTest("hello")也可以这样写:@AnnotationTest(value = "hello"),如果定义的参数不是"value"这个字符串,那么在使用的时候必须显式赋值,也就是必须用@AnnotationTest(value = "hello") 这样的模式,如String value1(); 那么对应的是@AnnotationTest(value1 = "hello") 因为value是个默认的,所以在使用是可以显式也可以隐式
java 代码
- package com.test;
- public @interface AnnotationTest {
- String value();
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest("hello")
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
3、带多个参数的定义与使用,请注意:
带多个参数的话,定义的时候用的是数组,使用的时候,可以是作为单个的也可以作为数组的形式
java 代码
- package com.test;
- public @interface AnnotationTest {
- String[] value();
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest("hello")
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest({"hello"})
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest({"hello","world"})
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest(value = {"hello","world"})
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
4、定义默认值的使用
java 代码
- package com.test;
- public @interface AnnotationTest {
- String value() default "yahaitt";
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest("hello")
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
java 代码
- package com.test;
- public @interface AnnotationTest {
- String value1() default "ttitfly";
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest(value1 = "hello")
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
4、枚举在Annotation中的使用
java 代码
- package com.test;
- public @interface AnnotationTest {
- EnumTest value1();
- }
- enum EnumTest
- {
- Hello,Yahaitt,Ttitfly
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest(value1 = EnumTest.Hello)
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
java 代码
- package com.test;
- public @interface AnnotationTest {
- EnumTest value1() default EnumTest.Yahaitt;
- }
- enum EnumTest
- {
- Hello,Yahaitt,Ttitfly
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
java 代码
- package com.test;
- public class AnnotationUsage {
- @AnnotationTest(value1 = EnumTest.Hello)
- public void method()
- {
- System.out.println("usage of annotation");
- }
- public static void main(String[] args)
- {
- AnnotationUsage au = new AnnotationUsage();
- au.method();
- }
- }
转自:http://wangyu.iteye.com/blog/210815