資訊型 註解

資訊型 註解

資訊型 註解

資訊型 註解

有時候透過註解提供一些基本資訊是非常有用的。
例如,下面這個註解說明了一個抽象方法回傳值。

// Returns an instance of the Responder being tested.
protected abstract Responder responderInstance();

這樣的註解是有幫助的,但用函式的名稱去傳達訊息會是更好的
作法。ex:將函式重新命名為responderBeingTested,原本的註解
就會變成多餘的。

還有個例子是
// format matched kk:mm:ss EEE, MMM dd, yyyy
Pattern timeMatcher = Pattern.compile(
“\d*:\d*:\d* \w*, \w*, \d*, \d*”);
這個例子的註解是用正規表示式來比對SimpleDateFormat.format
利用特定格式,格式化後的時間和日期。同樣地,更好的作法
是將程式碼移到一個特別的類別裡,而該類別可以轉換日期和
時間的格式,註解也可能是多餘的。