栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

创建自定义注释

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建自定义注释

如果创建自定义注释,则必须使用此处的

Reflection

API
示例进行处理。您可以参考如何声明注释。
这是Java中的示例注释声明的样子。


import java.lang.annotation.*;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface Test { }

Retention
并被
Target
称为
meta-annotations

RetentionPolicy.RUNTIME
表示您想在运行时保留注释,并且可以在运行时访问它。

ElementType.METHOD
表示您只能在方法上声明注释,类似地,您可以为类级别,成员变量级别等配置注释。

每个Reflection类都有获取声明的注释的方法。

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) getAnnotation(Class<T> annotationClass)Returns this element's annotation for the specified type if such an annotation is present, else null.public Annotation[] getDeclaredAnnotations()Returns all annotations that are directly present on this element. Unlike the other methods in this interface, this method ignores inherited annotations. (Returns an array of length zero if no annotations are directly present on this element.) The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.

你会发现这些方法的介绍

Field
Method
Class
类。

例如在运行时检索指定类上存在的注释

 Annotation[] annos = ob.getClass().getAnnotations();


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438695.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号