如果有多个可能的return语句,如何记录@return?

时间:2014-12-30 11:24:46

标签: java documentation return

如何记录此方法?返回值取决于if语句的结果。 我是否必须写下两种可能的回报以及它应该是什么样的?

/**
 * [...]
 *
 * @throws java.io.IOException 
 * @return aktivesKonto The Konto we looked for.
 * @return null If there wasn't an aktivesKonto
 */
public Konto getKonto(String kontonummer) throws java.io.IOException {

    if(...){
        System.out.println(...);

        return null;
    }
    else{

        return aktivesKonto;
    }
}
谢谢。

2 个答案:

答案 0 :(得分:0)

参见http://www.oracle.com/technetwork/articles/java/index-137868.html

的示例
/**
 * 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) {
        try {
            return getImage(new URL(url, name));
        } catch (MalformedURLException e) {
            return null;
        }
 }

答案 1 :(得分:0)

读者将阅读文档,如, 如果您撰写return null,则不会返回null,但您将返回值为object Konto的{​​{1}}。

并参考编码标准如果可能,只保留一个null语句,如下所示,建议使用。

return

所以在if语句中,如果你指定一些public Konto getKonto(String kontonummer) throws java.io.IOException { Konto aktivesKonto=null; if(...){ System.out.println(...); } return aktivesKonto; } 的值,它将返回相同的Konto