繁体   English   中英

注释类型与注释(Java)

[英]Annotation type vs Annotation (Java)

注释类型和注释本身之间的真正区别是什么。 我的老师告诉我要解决这个问题,因为他说这两个是2种不同的东西,我最好不要将它们混淆。

有人可以向我解释吗? 我在互联网上查找它,但没有发现任何用处。 我真的很高兴。

谢谢。 :)

@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface DBoperation {

    public enum ParamType { 
         FLOAT, INT, DOUBLE, CHAR, BOOLEAN, LONG, SHORT, BYTE, REFERENCE, NONE
     }

    public enum OperationType {
         CONNECT, DISCONNECT, UPDATE, QUERY, DELETE
     }

     public OperationType typOp() default OperationType.QUERY;
     public ParamType typParam() default ParamType.REFERENCE;
     String description();    
}

您的代码示例是注释类型的声明。 对于使用注释进行注释的每种方法,都会有一个注释,其中包含来自注释的数据。

您可以使用反射来获得注释,例如:

Class<?> annotatedClass = // put some class here
int methodIndex = // index of a annotated method
// get the annotation
// dbOperation is the annotation, DBoperation the type
DBoperation dbOperation = annotatedClass.getMethods()[methodIndex].getAnnotation(DBoperation.class);

// get data from the annotation 
String operationDescription = dbOperation.description();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM