标签:div col 代码示例 text 使用 inter for bsp none
@SuppressWarnings("unused") @Deprecated public class RealFood implements Food{ @SuppressWarnings("unused") @Deprecated @Override public void eat() { int x; System.out.println("吃饭 ,,,,,RealFood"); } }
import java.lang.annotation.Annotation; import com.hbsi.proxy.RealFood; public class TestDemo { public static void main(String[] args) { Class<?> cls = RealFood.class; Annotation[] annotations = cls.getAnnotations(); for (int i = 0; i < annotations.length; i++) { System.out.println(annotations[i]); } } }
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; //此注解在运行范围内有效 @Retention(value=RetentionPolicy.RUNTIME) public @interface MyAnnotation { public String name() default "hbsi";//定义name属性 public String values();//定义values属性 }
import com.hbsi.annotation.MyAnnotation; @MyAnnotation(name="hbsi",values="hebei") public class RealFood implements Food{ @Override public void eat() { int x; System.out.println("吃饭 ,,,,,RealFood"); } }
再次运行上述代码中获得Annotation 的类得到的结果如下:
import java.lang.annotation.Annotation; import com.hbsi.annotation.MyAnnotation; import com.hbsi.proxy.RealFood; public class TestDemo { public static void main(String[] args) { Class<?> cls = RealFood.class; Annotation[] annotations = cls.getAnnotations(); for (int i = 0; i < annotations.length; i++) { MyAnnotation annotation = cls.getAnnotation(MyAnnotation.class); System.out.println(annotation.name()); System.out.println(annotation.values()); System.out.println(annotation.annotationType()); } } }
执行结果
标签:div col 代码示例 text 使用 inter for bsp none
原文地址:http://www.cnblogs.com/wzqjy/p/7865395.html