//注解类
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String color()default "blue";
int [] arry() default {1,2,3};
}
//应用注解的类
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@MyAnnotation(color="red",arry={3,4,5},annotationAttr=@MetaAnnotation("hlw"))
public class AnnotationTest {
@MyAnnotation()
public static void main(String[] args)throws Exception{
//获取主函数
Method mainMethod = Annotation.class.getMethod("main",String[].class);
//获取主函数上的注解
MyAnnotation mainMyAtt = (MyAnnotation)mainMethod.getAnnotation(MyAnnotation.class);
//打印注解
System.out.println(mainMyAtt);
}
}
}
但是结果出错:Exception in thread "main" java.lang.NoSuchMethodException: java.lang.annotation.Annotation.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1773)
at cn.incast.day2.AnnotationTest.main(AnnotationTest.java:13)
难道不能在本类的主函数中用反射获得自身的主函数吗?
|
|