【问题标题】:How Exactly Does @param Work - Java@param 究竟是如何工作的 - Java
【发布时间】:2013-04-22 01:42:19
【问题描述】:

注解@param是如何工作的?

如果我有这样的事情:

/* 
*@param testNumber;
*/

int testNumber = 5;
if (testNumber < 6) {
   //Something
}

@param 将如何影响 testNumber?它甚至会影响 testNumber 吗?

谢谢。如果我用错了,请告诉我。

【问题讨论】:

  • /** 开头并以*/ 结尾的块仅由javadoc 处理。 Java 编译器将它们视为comments

标签: java param


【解决方案1】:

@paramjavadoc 用来生成文档的特殊格式注释。它用于表示方法可以接收的参数(或多个参数)的描述。还有@return@see分别用来描述返回值和相关信息:

http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html#format

除其他外,还有:

/**
 * Returns an Image object that can then be painted on the screen. 
 * The url argument must specify an absolute {@link URL}. The name
 * argument is a specifier that is relative to the url argument. 
 * <p>
 * This method always returns immediately, whether or not the 
 * image exists. When this applet attempts to draw the image on
 * the screen, the data will be loaded. The graphics primitives 
 * that draw the image will incrementally paint on the screen. 
 *
 * @param  url  an absolute URL giving the base location of the image
 * @param  name the location of the image, relative to the url argument
 * @return      the image at the specified URL
 * @see         Image
 */
 public Image getImage(URL url, String name) {

【讨论】:

    【解决方案2】:

    @param 不会影响号码。只是用来制作javadocs。

    更多关于 javadoc: http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html

    【讨论】:

    • 这是正确的,据我所知。你可以输入@param blahblah,它会让JD为参数blahblah生成。
    • 参数不影响方法。它会在查看方法的详细信息时向您显示您需要什么(在按下 后将鼠标悬停在方法上。)您甚至可以在 @param 之后添加更多信息以提供有关参数的更多信息
    【解决方案3】:

    @param 不会影响 testNumber。它是一个 Javadoc 注释 - 即用于生成文档。 您可以在类、字段、方法、构造函数或接口(例如@param@return)之前立即放置Javadoc 注释。 一般以 '@' 开头,并且必须是第一行。

    使用@param 的好处是:- 通过创建包含属性和一些自定义 Javadoc 标记的简单 Java 类,您可以让这些类用作代码生成的简单元数据描述。

        /* 
           *@param testNumber
           *@return integer
        */
        public int main testNumberIsValid(int testNumber){
    
           if (testNumber < 6) {
              //Something
            }
         }
    

    只要在您的代码中重用 testNumberIsValid 方法,IDE 就会向您显示该方法接受的参数和该方法的返回类型。

    【讨论】:

      【解决方案4】:

      您可能会错过@author,并且在@param 中您需要解释该参数的用途、使用方法等。

      【讨论】:

      • 哦,对不起,我的意思是@param
      【解决方案5】:

      它基本上是一个评论。众所周知,从事同一个项目的许多人必须了解代码更改。我们正在程序中对参数做一些注释。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-26
        • 2021-08-15
        • 2012-06-08
        • 2011-10-11
        • 2013-07-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多