2011-12-31 46 views
2

我想學習在我的課程中編寫方法定義。即:Java寫作方法定義

public int myMethod() 
    { 
    //This method is used for ....bla bla bla.... 
    } 

我想通知用戶什麼方法。 在.Net中,您可以編寫該定義,並且您可以在編寫該方法時看到解釋。如何在JAVA中完成?

回答

3

充實了一些其他的答案的鏈接瞭解更多。

第一句話應該是第三人稱陳述句,回答「這個方法做什麼」的問題,e.q.,「創建一個foobar」。此外,第一句用作摘要評論,因此應該儘可能清晰和簡潔。

例如,如果在一個文件中讀取你的方法,並返回一個整數狀態:

/** 
* Reads in config file and initializes application. 
* 
* @return Application status; 0 if everything is okay. 
*/ 
public int myMethod() { 
    // ... 
} 

IMO增加不必要的細節就是這樣 - 沒有必要。一些方法是自我記錄,規範的例子是獲取者/設置者:

/** 
* Sets first name. 
* 
* @param firstName Name to set. 
*/ 
public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

冗餘評論。同樣,良好的命名方法,可避免需要大量的,或任何文檔:

public List<User> getAllUsers() { ... } 
public User findUserById(Long id) { ... } 

IMO,除非有什麼東西實際上顯着,有沒有必要的話。

HTML用於標記Javadoc,但IMO最好將其格式化爲可以多種格式(編輯器,IDE,Javadocs等)讀取的格式,所以我傾向於縮進和使用空格以確保我可以以純文本格式和渲染方式查看所有內容。

標準doclet假定HTML:除非通過<p><br>標籤明確指出,否則將忽略空白。

/** 
* Builds and returns the current list of ingredients. 
* 
* <p> 
* <b>Note:</b> Initializes ingredient information if necessary. 
* </p> 
*/ 

相關鏈接:

+0

謝謝。這很好... – ahmet 2011-12-31 18:30:08

4

做到這一點的方法:

/** 
* This method is used for.. 
*/ 
public int myMethod() 
{ 

} 

和PARAMS使用這樣的:

/** 
* This method is used for.. 
* @param v pass this to do something 
*/ 
public int myMethod(Object v) 
{ 

} 
這裏

完整的細節:oracle.com

2

這是有利的,使用Javadoc功能之前,如果你的絕筆它。

/** 
* Does [fill in the blank here] 
* @return An integer stating [what it does] 
*/ 
public int myMethod() { 
    // Fill in the rest here 
} 
2

添加它作爲一個上面的方法/ ** ... * /註釋:

/** 
* This method is used for ....bla bla bla.... 
*/ 
public int myMethod() 
{ 

} 

Eclipse會,一旦你鍵入/ **自動生成一個javadoc方法簽名留言,並點擊回車去到下一行。

+0

誰保證該人正在使用IDE(更不用說Eclipse)? – Makoto 2011-12-31 18:05:41

+0

@Makoto JavaDoc不是IDE特定的,你甚至不需要一個 - http://en.wikipedia.org/wiki/Javadoc - 通常它變成HTML,如下所示:http://docs.oracle.com/javase /1.5.0/docs/api/java/lang/String.html – 2011-12-31 18:08:01

+0

@Makoto沒有保證,只是說Eclipse會這麼做,也可能是Netbeans/IntelliJ。我不確定爲什麼不使用IDE,除非他們不知道更好,不喜歡提供方便和提高開發速度的工具,否則相信禪是通過在vi/emacs/Textpad/Notepad/Xcode中開發來實現的的/ etc /等。 – 2011-12-31 18:09:44

0

您需要使用javadoc註釋才能完成此操作。像下面

/** * This method is used for.. */ public int myMethod() { } 

的東西在這裏是Javadoc的方法Javadoc methods