Java - 编程规范(Programming Convention)

本文详细介绍了Java编程中的命名常量规范,包括全大写加下划线的命名方式,并强调了常量的位置。此外,讨论了注释的使用,如行注释、块注释以及Javadoc的使用,以生成项目的文档。遵循这些编程规范能助你成为专业程序员。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 whole
process of playing
the 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值