/** * A class rePResenting a set of packet and byte counters * It is observable to allow it to be watched, but only * reports changes when the current set is complete */ 接下來是類定義,包含了在不同的行的 extends 和 implements : public class CounterSet extends Observable implements Cloneable ● Class Fields 接下來是類的成員變量: /** * Packet counters */ protected int[] packets; public 的成員變量必須生成文檔(JavaDoc)。proceted、private和 package 定義的成員變量假如名字含義明確的話,可以沒有注釋。 ● 存取方法 接下來是類變量的存取的方法。它只是簡單的用來將類的變量賦值獲取值的話,可以簡單的寫在一行上。 /** * Get the counters * @return an array containing the statistical data. This array has been * freshly allocated and can be modified by the caller. */ public int[] getPackets() { return copyArray(packets, offset); } public int[] getBytes() { return copyArray(bytes, offset); } public int[] getPackets() { return packets; } public void setPackets(int[] packets) { this.packets = packets; } 其它的方法不要寫在一行上。 ● 構造函數