@interface的作用是自定义一个新的注解
例子:
package com.self;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTarget
{ }
使用新定义的注解@MyTarget
package com.self;
import java.lang.reflect.Method;
public class MyTargetTest
{
@MyTarget
public void doSomething()
{
System.out.println("hello world");
}
public static void main(String[] args) throws Exception
{
Method method = MyTargetTest.class.getMethod("doSomething",null);
if(method.isAnnotationPresent(MyTarget.class))//如果doSomething方法上存在注解@MyTarget,则为true
{
System.out.println(method.getAnnotation(MyTarget.class));
}
}
}