<legend id='r2CbI'><style id='r2CbI'><dir id='r2CbI'><q id='r2CbI'></q></dir></style></legend>

      <bdo id='r2CbI'></bdo><ul id='r2CbI'></ul>
    <i id='r2CbI'><tr id='r2CbI'><dt id='r2CbI'><q id='r2CbI'><span id='r2CbI'><b id='r2CbI'><form id='r2CbI'><ins id='r2CbI'></ins><ul id='r2CbI'></ul><sub id='r2CbI'></sub></form><legend id='r2CbI'></legend><bdo id='r2CbI'><pre id='r2CbI'><center id='r2CbI'></center></pre></bdo></b><th id='r2CbI'></th></span></q></dt></tr></i><div id='r2CbI'><tfoot id='r2CbI'></tfoot><dl id='r2CbI'><fieldset id='r2CbI'></fieldset></dl></div>

    <small id='r2CbI'></small><noframes id='r2CbI'>

  1. <tfoot id='r2CbI'></tfoot>

      Java 中的@interface 默认声明用法

      时间:2023-07-26

      <tfoot id='JCDxn'></tfoot>

        <i id='JCDxn'><tr id='JCDxn'><dt id='JCDxn'><q id='JCDxn'><span id='JCDxn'><b id='JCDxn'><form id='JCDxn'><ins id='JCDxn'></ins><ul id='JCDxn'></ul><sub id='JCDxn'></sub></form><legend id='JCDxn'></legend><bdo id='JCDxn'><pre id='JCDxn'><center id='JCDxn'></center></pre></bdo></b><th id='JCDxn'></th></span></q></dt></tr></i><div id='JCDxn'><tfoot id='JCDxn'></tfoot><dl id='JCDxn'><fieldset id='JCDxn'></fieldset></dl></div>
      1. <small id='JCDxn'></small><noframes id='JCDxn'>

      2. <legend id='JCDxn'><style id='JCDxn'><dir id='JCDxn'><q id='JCDxn'></q></dir></style></legend>
        • <bdo id='JCDxn'></bdo><ul id='JCDxn'></ul>
            <tbody id='JCDxn'></tbody>

                本文介绍了Java 中的@interface 默认声明用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我刚刚发现了这个功能.

                I have just discovered this feature.

                使用@interface"语法声明接口允许您放置默认值.

                Declaring an interface using the "@interface" syntax allows you to put a default value.

                public @interface HelloWorld { 
                     public String sayHello() default "hello world";
                }
                

                这对我来说是新事物.假设如何使用该默认值.

                This is something new for me. How is that default value suppose to be used.

                我找不到对此的引用,因为在 Java 1.5 中添加@"之前,www 中充满了 java 接口文档(是在 .5 还是在 .4?)

                I cannot find references to that, because the www is full of java interface documents prior to "@" addition in Java 1.5 ( was it on .5 or in .4? )

                编辑

                感谢您的回答(我有点接近注释",因为我已经使用了标签):P

                几年前我就知道我应该阅读该文件!!!...让我们看看...

                I knew I should've read that document years ago!!!... let's see...

                许多 API 需要相当多的样板代码.对于....

                推荐答案

                你刚刚写了一个注解.

                特别关于 default 语句:这是因为注解和接口不能有构造函数,所以这是为注解属性设置默认值的唯一方法.来自 Java 语言规范:

                Regarding the default statement in particular: This is used because annotations and interfaces can't have constructors, so this is the only way to have a default value for an annotation attribute. From the Java Language Specification:

                注解类型元素可以有一个为其指定的默认值.这是通过在其(空)参数列表后面加上关键字 default 和元素的默认值来完成的.

                An annotation type element may have a default value specified for it. This is done by following its (empty) parameter list with the keyword default and the default value of the element.

                在读取注释时动态应用默认值;默认值不会编译到注释中.因此,更改默认值会影响注释,即使在更改之前编译的类中也是如此(假设这些注释缺少默认元素的显式值).

                Defaults are applied dynamically at the time annotations are read; default values are not compiled into annotations. Thus, changing a default value affects annotations even in classes that were compiled before the change was made (presuming these annotations lack an explicit value for the defaulted element).

                我注意到 java.lang.annotation 不过使用默认值.

                I note that none of the annotations in java.lang.annotation use default values, though.

                用法:你有一个注解@HelloWorld和一个属性sayHello.你可以把它放在这样的类上:

                Usage: You have an annotation @HelloWorld with an attribute sayHello. You could put it on a class like this:

                @HelloWorld(sayHello="Hi")
                public class MyClass {
                }
                

                因为你有一个默认值,你可以放

                Since you have a default value, you could just put

                @HelloWorld
                public class MyClass {
                }
                

                (请注意,文档中说,在带有单个元素的注释中,该元素应命名为 value";我认为这样做的唯一原因是您可以只写 @HelloWorld("Hi") 无需命名参数.)

                (Note that the document says, "In annotations with a single element, the element should be named value"; I believe the only reason to do this is that you could just write @HelloWorld("Hi") without having to name the parameter.)

                正如所写,您的注释可用于任何有效的程序元素(包括方法和变量声明).您可以使用 @Target 更改它 注释.

                As written, your annotation can be used on any valid program element (including methods and variable declarations). You can change this with the @Target annotation.

                最后,设置 RetentionPolicy 让您决定注解是应该被编译器丢弃、被 VM 丢弃还是一直保留.

                Finally, setting the RetentionPolicy lets you decide if the annotation should be discarded by the compiler, discarded by the VM, or kept always.

                两个可能也很有趣的包:javax.annotation 和 javax.annotation.processing.和 这里是使用注释处理进行源代码分析的示例.

                Two packages that might also be interesting: javax.annotation and javax.annotation.processing. And here is an example of using annotation processing for source code analysis.

                这篇关于Java 中的@interface 默认声明用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 @Column JPA 注释上设置长度属性时有什么作用 下一篇:如何创建作为一组杰克逊注释的注释?

                相关文章

                最新文章

                  <bdo id='gDFHY'></bdo><ul id='gDFHY'></ul>

                1. <small id='gDFHY'></small><noframes id='gDFHY'>

                    <legend id='gDFHY'><style id='gDFHY'><dir id='gDFHY'><q id='gDFHY'></q></dir></style></legend><tfoot id='gDFHY'></tfoot>
                  1. <i id='gDFHY'><tr id='gDFHY'><dt id='gDFHY'><q id='gDFHY'><span id='gDFHY'><b id='gDFHY'><form id='gDFHY'><ins id='gDFHY'></ins><ul id='gDFHY'></ul><sub id='gDFHY'></sub></form><legend id='gDFHY'></legend><bdo id='gDFHY'><pre id='gDFHY'><center id='gDFHY'></center></pre></bdo></b><th id='gDFHY'></th></span></q></dt></tr></i><div id='gDFHY'><tfoot id='gDFHY'></tfoot><dl id='gDFHY'><fieldset id='gDFHY'></fieldset></dl></div>