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); }