Java

来源:互联网 发布:校园网mac地址克隆 编辑:程序博客网 时间:2024/05/18 01:11

Java - 编程规范(Programming Convention)


常量命名(Naming Contants)
Spell named constants using all uppercase letters with the underscore used to seperate “words”. For example,

public static final double INTREST_RATE = 2.5;public static final int MAX_SPEED = 120;public static final char GOAL = 'A';public static final String GREETING = "Welcome";

Those statements must be placed outside of any methods including main method. However, if a constant is going to be used inside a single method, the it can be defined inside the method, for example:

public void method(){    double INTREST_RATE = 2.5;    System.out.println("The interest rate is "+INTREST_RATE);}

注释(Comments)
line comment: All of the text between the // and the end of line is a line comment. For example,

int speed;//This represents the speed of a running vehicle

Block comment: Anything between the pair /* and the pair */ is block comment. For example,

/* This method controls the wholeprocess of playingthe nim game */public void playGame(){    ...}

In order to generate comment documents of a Java project using Javadoc, we usually start a block comment with /**. For example,

    /**     * Allocates a new {@code String} that contains characters from a subarray     * of the character array argument. The {@code offset} argument is the     * index of the first character of the subarray and the {@code count}     * argument specifies the length of the subarray. The contents of the     * subarray are copied; subsequent modification of the character array does     * not affect the newly created string.     *     * @param  value     *         Array that is the source of characters     *     * @param  offset     *         The initial offset     *     * @param  count     *         The length     *     * @throws  IndexOutOfBoundsException     *          If the {@code offset} and {@code count} arguments index     *          characters outside the bounds of the {@code value} array     */    public String(char value[], int offset, int count) {        if (offset < 0) {            throw new StringIndexOutOfBoundsException(offset);        }        if (count <= 0) {            if (count < 0) {                throw new StringIndexOutOfBoundsException(count);            }            if (offset <= value.length) {                this.value = "".value;                return;            }        }        // Note: offset or count might be near -1>>>1.        if (offset > value.length - count) {            throw new StringIndexOutOfBoundsException(offset + count);        }        this.value = Arrays.copyOfRange(value, offset, offset+count);    }

We could use Javadoc to automatically generate comment documents. You can find it in eclipse: Project -> Generate Javadoc.

更多规范(More Conventions)
There are three documents which are the conventions of development of the NIM Game.

最后再说两句(PS)
Those programming convention will help you become a professional programmer. So, pay attention to that.
Welcome questions always and forever.

原创粉丝点击