首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

引语入门实例

2012-06-30 
注解入门实例package com.csair.antoimport java.lang.reflect.Methodpublic class RunAnto {/* * 被注

注解入门实例

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();}

?

热点排行