Java基础(六)----注释 annotation
来源:互联网 发布:sap创建物料主数据 编辑:程序博客网 时间:2024/06/05 07:13
共分为4个类
FirstAnno.class
import java.lang.annotation.*;@Documented @Inherited // 可以继承@Retention(RetentionPolicy.RUNTIME) // 保留策略@Target(ElementType.TYPE) // 修饰目标public @interface FirstAnno { String value() default "FirstAnno";}
SecondAnnotation.class
import java.lang.annotation.*;@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface SecondAnnotation { String name() default "Hrmzone"; String url() default "hrmzone.cn";}
Kitto.class
import java.lang.annotation.*;@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)public @interface Kitto { String value() default "kitto";}
测试mian类
@FirstAnno("http://hrmzone.cn")public class AnnoTest { @Kitto("测试") private String test = ""; @SecondAnnotation public String getDefault() { return "get default annotation"; } @SecondAnnotation(name="desktophrm", url="desktophrm.com") public String getDefine() { return "get define annotation"; } public static void main (String[] args) throws ClassNotFoundException { Class<?> c = Class.forName("AnnoTest"); Method[] methods = c.getMethods(); boolean flag = c.isAnnotationPresent(FirstAnno.class); if ( flag ) { FirstAnno first = c.getAnnotation(FirstAnno.class); System.out.println("FirstAnno = " + first.value()); } for ( Method m : methods ) { SecondAnnotation second = m.getAnnotation(SecondAnnotation.class); if ( second == null ) continue; System.out.println("SecondAnnotation = " + second.name() + "; " + second.url()); } Field[] fs = c.getDeclaredFields(); for ( Field f : fs ) { Kitto kitto = f.getAnnotation(Kitto.class); if ( kitto == null ) continue; System.out.println("Kitto = " + kitto.value()); } Class<?> c1 = Class.forName("AnnoTestInherited";) }}class AnnoTestInherited extends AnnoTest {}
阅读全文
0 0
- Java基础(六)----注释 annotation
- Java注释(Annotation)
- Java注释(Annotation)
- Java :Annotation(注释)
- 16. Java Annotation(注释)
- java的Annotation(注释)
- Java-----12、Annotation(注释)
- Java Annotation注释语法(元注释)
- Java Annotation注释语法
- Java Annotation注释语法
- java annotation 注释 简介
- Java注释(Annotation)详解
- Java注释(Annotation)详解
- Annotation:java注释
- Java Annotation注释语法
- Java注释Annotation
- Java注释Annotation
- Java注释(Annotation)详解
- 计算机编码
- Matlab实现svm的分类
- 相对定位与绝对定位
- mysql 优化语句
- 引用作为函数返回值
- Java基础(六)----注释 annotation
- Shell特殊变量大全: $0, $#, $*, $@, $?, $$,$!,$- 简介.$@ $*对比详解 -- shell学习
- 搬瓦工VPS搭建VPN轻松访问Google等
- H5文章文字之间的间距、行距等属性
- STM32 TIM2通道重映射 PWM 无输出波形
- Poj 2449 第k短路
- python中append和extend函数区别
- Asis CTF 2013: "RSAng"
- hdu 6096 String(详解)