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

977

被折叠的 条评论
为什么被折叠?



