帮我理解Java注释 [英] help me understanding annotation in java

查看:123
本文介绍了帮我理解Java注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图去通过一些网上的材料,学习Java注释在以下code任何一个可以请让我知道发生了什么事我亲爱的Hello World的字符串,我在这行通过: @Test_Target(doTestTarget =的Hello World!)

I was trying to go through some online material to learn annotation in java In the following code can any one please let me know what happened to my dear "Hello world" string which i passed in this line: @Test_Target(doTestTarget="Hello World !")

@Target(ElementType.METHOD)
public @interface Test_Target {
   public String doTestTarget();
}

以上定义,下面的注释是它的使用

above is the annotation defined and below is its usage

public class TestAnnotations {
   @Test_Target(doTestTarget="Hello World !")
   private String str;
   public static void main(String arg[]) {
      new TestAnnotations().doTestTarget();
   }
   public void doTestTarget() {
      System.out.printf("Testing Target annotation");
   }
}

当我运行它...它只是打印测试目标注释结果
请帮我,我完全新的@annotation

When I am running it ...it is only printing Testing Target annotation
Please help me out, I am completely new to @annotation

在此先感谢:)

推荐答案

注释基本上都是数据的位数,你可以连接到字段,方法,类等等。

Annotations are basically bits of data you can attach to fields, methods, classes, etc.

在Java注释声明的语法是有点尴尬。它们看上去有点像接口(他们,毕竟,与 @interface 声明),但他们不是真正的接口。我想你可能已经把 doTestTarget()方法在 TestAnnotations 类,因为你认为你的注释是一个接口,你需要实现它。这是不正确的 - 你可以从你的code,如果你想和这样做不会给你任何问题,删除此方法,并调用它

The syntax for declaring annotations in Java is a little awkward. They look a bit like interfaces (they are, after all, declared with @interface), but they aren't really interfaces. I think you might have put the doTestTarget() method in your TestAnnotations class because you thought your annotation was an interface and you needed to implement it. This isn't true - you can delete this method and the call to it from your code if you wish and doing so won't cause you any problems.

另外,你可能没有打算把注释场上 STR 。注释仅适用于什么,立刻跟随他们。这样一来,你的code不能编译,因为你已经应用于您的注释字段,但声明自己的注释只能应用于方法。更改 @Target(ElementType.METHOD) @Target(ElementType.FIELD)和你的code必须再编译。

Also, you might not have intended to put the annotation on the field str. Annotations apply only to what immediately follows them. As a result, your code doesn't compile, because you've applied your annotation to a field but declared that your annotation can only be applied to methods. Change @Target(ElementType.METHOD) to @Target(ElementType.FIELD) and your code should then compile.

至于会发生什么情况字符串的Hello World!,它被写入到.class文件,并提供给任何工具,在Java类中读取。然而,这不一定是在运行时在JVM可用。这是因为你没有指定 @Retention @Test_Target 注释。为 @Retention 默认值为 RetentionPolicy.CLASS ,这意味着JVM大可不必去加载它们出类文件。 (请参阅<一个href=\"http://download.oracle.com/javase/6/docs/api/java/lang/annotation/RetentionPolicy.html\">Javadoc为的RetentionPolicy枚举。)

As for what happens to the string Hello World !, it gets written to the .class file and is available to any tool that reads in Java classes. However, it wouldn't necessarily be available in the JVM at runtime. This happens because you didn't specify a @Retention for your @Test_Target annotation. The default value for @Retention is RetentionPolicy.CLASS, which means that the JVM might not bother to load them out of the class file. (See the Javadoc for the RetentionPolicy enum.)

我想你想看到的在运行时读取数值超出这个注释的一些方法。如果是的话,我建议你加入 @Retention(RetentionPolicy.RUNTIME)您的注释,以确保它会在运行时可用。

I imagine you want to see some way of reading the value out of this annotation at runtime. If so, I'd recommend adding @Retention(RetentionPolicy.RUNTIME) to your annotation to make sure it will be available at runtime.

要在运行时访问其中包含的注记和价值,就需要使用反射。我已经重写你的 TestAnnotations 类,如下所示给一个快速演示:

To access your annotation and the value contained within it at runtime, you need to use reflection. I've rewritten your TestAnnotations class as follows to give a quick demonstration:

import java.lang.reflect.Field;

public class TestAnnotations {

   @Test_Target(doTestTarget="Hello World !")
   private String str;

   public static void main(String[] args) throws Exception {
      // We need to use getDeclaredField here since the field is private.
      Field field = TestAnnotations.class.getDeclaredField("str");
      Test_Target ann = field.getAnnotation(Test_Target.class);
      if (ann != null) {
         System.out.println(ann.doTestTarget());
      }
   }
}

当我运行这个code,它给了我下面的输出:

When I run this code, it gives me the following output:


Hello World !

这篇关于帮我理解Java注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆