电脑技术学习

Java程序的编码规范(2)

dn001
内容: Java程序的编码规范(2)
作者:李小敏 本文选自:IBM DW中国 2002年08月21日

其他不需要出现在 javadoc 的信息也可以包含在这里。

● Package/Imports

package 行要在 import 行之前,import 中标准的包名要在本地的包名之前,而且按照字母顺序排列。如果 import 行中包含了同一个包中的不同子目录,则应该用 * 来处理。

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




其它的方法不要写在一行上。
Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd

标签: