Package 的命名 Package 的名字應該都是由一個小寫單詞組成。 Class 的命名 Class 的名字必須由大寫字母開頭而其他字母都小寫的單詞組成 Class 變量的命名 變量的名字必須用一個小寫字母開頭。后面的單詞用大寫字母開頭。 Static Final 變量的命名 Static Final 變量的名字應該都大寫,并且指出完整含義。 參數的命名 參數的名字必須和變量的命名規范一致。 數組的命名 數組應該總是用下面的方式來命名:
byte[] buffer;
而不是:
byte buffer[];
方法的參數 使用有意義的參數命名,假如可能的話,使用和要賦值的字段一樣的名字:
SetCounter(int size){ this.size = size; }
Java 文件樣式 所有的 Java(*.java) 文件都必須遵守如下的樣式規則
版權信息 版權信息必須在 java 文件的開頭,比如:
/** * Copyright ?2000 Shanghai XXX Co. Ltd. * All right reserved. */
這里 java.io.* 使用來代替InputStream and OutputStream 的。 Class 接下來的是類的注釋,一般是用來解釋類的。
/** * 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); }