JAVA命名规范

JAVA命名规范

  关于Java中各种元素的命名,定义这些规范的目的是让项目中所有的文档都看起来像一个人写的,增加可读性,减少项目组中因为换人而带来的损失。(这些规范并不是一定要绝对遵守,但是一定要让程序有良好的可读性):

    1. Package 的命名
      Package 的名字应该都是由一个小写单词组成。

    2. Class 的命名
      Class 的名字必须由大写字母开头而其他字母都小写的单词组成。

    3. Class 变量的命名
      变量的名字必须用一个小写字母开头。后面的单词用大写字母开头。

    4. Static Final 变量的命名
      Static Final 变量的名字应该都大写,并且指出完整含义。

    5. 参数的命名
      参数的名字必须和变量的命名规范一致。

    6. 数组的命名
      数组应该总是用下面的方式来命名:
      1   byte[] buffer; 
      而不是:
      1   byte buffer[];
    7. 方法的参数
      使用有意义的参数命名,如果可能的话,使用和要赋值的字段一样的名字:
      1   SetCounter(int size){
      2   this.size = size;
      3   }
    8. Java 文件样式
      所有的 Java(*.java) 文件都必须遵守如下的样式规则
        版权信息必须在 java 文件的开头,比如:
      1   /**
      2   * Copyright ?0?3 2000 Shanghai XXX Co. Ltd.
      3   * All right reserved.
      4   */ 
        其他不需要出现在 javadoc 的信息也可以包含在这里。
    9. Package/Imports
      package 行要在 import 行之前,import 中标准的包名要在本地的包名之前,而且按照字母顺序排列。
      如果 import 行中包含了同一个包中的不同子目录,则应该用 * 来处理。
      1   package hotlava.net.stats;
      2   import java.io.*;
      3   import java.util.Observable;
      4   import hotlava.util.Application; 
      这里 java.io.* 使用来代替InputStream and OutputStream 的。
    10. Class
      接下来的是类的注释,一般是用来解释类的。
      1   /**
      2   * A class representing a set of packet and byte counters
      3   * It is observable to allow it to be watched, but only
      4   * reports changes when the current set is complete
      5   */
      接下来是类定义,包含了在不同的行的 extends 和 implements
      1   public class CounterSet
      2   extends Observable
      3   implements Cloneable
      4   Class Fields 
      接下来是类的成员变量:
      1   /**
      2   * Packet counters
      3   */
      4   protected int[] packets; 
      public 的成员变量必须生成文档(JavaDoc)。proceted、private和 package 定义的成员变量如果名字含义明确的话,可以没有注释。
    11. 存取方法
      接下来是类变量的存取的方法。它只是简单的用来将类的变量赋值获取值的话,可以简单的写在一行上。
      1   /**
      2   * Get the counters
      3   * @return an array containing the statistical data. This array has been
      4   * freshly allocated and can be modified by the caller.
      5   */
      6   public int[] getPackets() { return copyArray(packets, offset); }
      7   public int[] getBytes() { return copyArray(bytes, offset); }
      8   public int[] getPackets() { return packets; }
      9   public void setPackets(int[] packets) { this.packets = packets; } 
      其它的方法不要写在一行上
    12. 构造函数
      接下来是构造函数,它应该用递增的方式写(比如:参数多的写在后面)。
      访问类型 (“public”, “private” 等.) 和 任何 “static”, “final” 或 “synchronized” 应该在一行中,并且方法和参数另写一行,这样可以使方法和参数更易读。
      1   public
      2   CounterSet(int size){
      3   this.size = size;
      4   }
    13. 克隆方法
      如果这个类是可以被克隆的,那么下一步就是 clone 方法:
       1   public
       2   Object clone() {
       3   try {
       4   CounterSet obj = (CounterSet)super.clone();
       5   obj.packets = (int[])packets.clone();
       6   obj.size = size;
       7   return obj;
       8   }catch(CloneNotSupportedException e) {
       9   throw new InternalError(“Unexpected CloneNotSUpportedException: ” + e.getMessage());
      10   }
      11   }
    14. 类方法
      下面开始写类的方法:
       1   /**
       2   * Set the packet counters
       3   * (such as when restoring from a database)
       4   */
       5   protected final
       6   void setArray(int[] r1, int[] r2, int[] r3, int[] r4)
       7   throws IllegalArgumentException
       8   {
       9   // Ensure the arrays www.gzlij.com are of equal size
      10 if (r1.length != r2.length || r1.length != r3.length || r1.length != r4.length)
      11   throw new IllegalArgumentException(“Arrays must be of the same size”);
      12   System.arraycopy(r1, 0, r3, 0, r1.length);
      13   System.arraycopy(r2, 0, r4, 0, r1.length);
      14   }
    15. toString 方法
      无论如何,每一个类都应该定义 toString 方法:
       1   public
       2   String toString() {
       3   String retval = “CounterSet: “;
       4   for (int i = 0; i < data.length(); i++) {
       5   retval += data.bytes.toString();
       6   retval += data.packets.toString();
       7   }
       8   return retval;
       9   }
      10   }
    16. main 方法
      如果main(String[]) 方法已经定义了, 那么它应该写在类的底部.
posted @ 2017-12-29 23:59  王俊玺  阅读(2294)  评论(0编辑  收藏  举报