注解入门实例
package com.csair.anto;import java.lang.reflect.Method;public class RunAnto {/* * 被注解的三个方法 */@Student(userName = "william", address = "广东省东莞市")public void method_1() {}@Student(userName = "媛媛", address = "")public void method_2() {}@Student(address = "北京市")public void method_3() {}/* * 解析注解,将RunAnto类 所有被注解方法 的信息打印出来 */public static void main(String[] args) {Method[] methods = RunAnto.class.getDeclaredMethods();for(Method method : methods){boolean hasAnnotation = method.isAnnotationPresent(Student.class);if(hasAnnotation){Student annotation = method.getAnnotation(Student.class);System.out.println(method.getName() + " " + annotation.userName() + " " +annotation.address());}}}} ?
package com.csair.anto;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface Student {String userName() default "admin";String address();}?