1、所有的程序开发手册都包含了各种规则。一些习惯自由程序人员可能对这些规则很不适应,但是在多个开发人员共同写作的情况下,这些规则是必需的。这不仅仅是为了开发效率来考虑,而且也是为了后期维护考虑。 命名规范 定义这个规范的目的是让项目中所有的文档都看起来像一个人写的,增加可读性,减少项目组中因为换人而带来的损失。(这些规范并不是一定要绝对遵守,但是一定要让程序有良好的可读性) Package 的命名 Package 的名字应该都是由一个小写单词组成。 Class 的命名 Class 的名字必须由大写字母开头而其他字母都小写的单词组成 Class 变量
4、 package hotlava.net.stats; import java.io.*; import java.util.Observable; import hotlava.util.Application; 这里 java.io.* 使用来代替InputStream and OutputStream 的。 Class 接下来的是类的注释,一般是用来解释类的。 /** * A class representing a set of packet and byte counters * It is observable to allow i
5、t 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 的成员
6、变量必须生成文档(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. */
7、 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; } 其它的方法不要写在一行上 构造函数 接下来是构造函数,它应该用递增的方式写(比