原文出处:http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html,
译文出处:http://morningspace.51.net/,[email protected]
文件类别
|
文件后缀
|
Java源文件
|
.java
|
Java字节码文件
|
.class
|
文件名
|
用途
|
GNUmakefile
|
makefiles的首选文件名。我们采用gnumake来创建(build)软件。
|
README
|
概述特定目录下所含内容的文件的首选文件名
|
/*
* Classname
*
* Version information
*
* Date
*
* Copyright notice
*/
package java.awt;
import java.awt.peer.CanvasPeer;
类/接口声明的各部分
|
注解
|
|
1
|
类/接口文档注释(/**……*/)
|
该注释中所需包含的信息,参见"
文档注释"
|
2
|
类或接口的声明
|
|
3
|
类/接口实现的注释(/*……*/)如果有必要的话
|
该注释应包含任何有关整个类或接口的信息,而这些信息又不适合作为类/接口文档注释。
|
4
|
类的(静态)变量
|
首先是类的公共变量,随后是保护变量,再后是包一级别的变量(没有访问修饰符,access modifier),最后是私有变量。
|
5
|
实例变量
|
首先是公共级别的,随后是保护级别的,再后是包一级别的(没有访问修饰符),最后是私有级别的。
|
6
|
构造器
|
|
7
|
方法
|
这些方法应该按功能,而非作用域或访问权限,分组。例如,一个私有的类方法可以置于两个公有的实例方法之间。其目的是为了更便于阅读和理解代码。
|
someMethod(longExpression1, longExpression2, longExpression3,
longExpression4, longExpression5);
var = someMethod1(longExpression1,
someMethod2(longExpression2,
longExpression3));
longName1 = longName2 * (longName3 + longName4 - longName5)
+ 4 * longname6; //PREFFER
longName1 = longName2 * (longName3 + longName4
- longName5) + 4 * longname6; //AVOID
//CONVENTIONAL INDENTATION
someMethod(int anArg, Object anotherArg, String yetAnotherArg,
Object andStillAnother) {
...
}
//INDENT 8 SPACES TO AVOID VERY DEEP INDENTS
private static synchronized horkingLongMethodName(int anArg,
Object anotherArg, String yetAnotherArg,
Object andStillAnother) {
...
}
//DON’T USE THIS INDENTATION
if ((condition1 && condition2)
|| (condition3 && condition4)
||!(condition5 && condition6)) { //BAD WRAPS
doSomethingAboutIt(); //MAKE THIS LINE EASY TO MISS
}
//USE THIS INDENTATION INSTEAD
if ((condition1 && condition2)
|| (condition3 && condition4)
||!(condition5 && condition6)) {
doSomethingAboutIt();
}
//OR USE THIS
if ((condition1 && condition2) || (condition3 && condition4)
||!(condition5 && condition6)) {
doSomethingAboutIt();
}
alpha = (aLongBooleanExpression) ? beta : gamma;
alpha = (aLongBooleanExpression) ? beta
: gamma;
alpha = (aLongBooleanExpression)
? beta
: gamma;
/*
* Here is a block comment.
*/
/*-
* Here is a block comment with some very special
* formatting that I want indent(1) to ignore.
*
* one
* two
* three
*/
if (condition) {
/* Handle the condition. */
...
}
if (a == 2) {
return TRUE; /* special case */
} else {
return isPrime(a); /* works only for odd a */
}
if (foo > 1) {
// Do a double-flip.
...
}
else {
return false; // Explain why here.
}
//if (bar > 1) {
//
// // Do a triple-flip.
// ...
//}
//else {
// return false;
//}
/**
* The Example class provides ...
*/
public class Example { ...
int level;// indentation level
int size; // size of table
int foo,fooarray[]; //WRONG!
int level; // indentation level
int size; // size of table
Object currentEntry;// currently selected table entry
void myMethod() {
int int1 = 0; // beginning of method block
if (condition) {
int int2 = 0; // beginning of "if" block
...
}
}
for (int i = 0; i < maxLoops; i++) { ... }
int count;
...
myMethod() {
if (condition) {
int count = 0; // AVOID!
...
}
...
}
class Sample extends Object {
int ivar1;
int ivar2;
Sample(int i, int j) {
ivar1 = i;
ivar2 = j;
}
int emptyMethod() {}
...
}
argv++; // Correct
argc--; // Correct
argv++; argc--; // AVOID!
return;
return myDisk.size();
return (size ? size : defaultSize);
if (condition) {
statements;
}
if (condition) {
statements;
} else {
statements;
}
if (condition) {
statements;
} else if (condition) {
statements;
} else{
statements;
}
if (condition) //AVOID! THIS OMITS THE BRACES {}!
statement;
for (initialization; condition; update) {
statements;
}
for (initialization; condition; update);
while (condition) {
statements;
}
while (condition);
do {
statements;
} while (condition);
switch (condition) {
case ABC:
statements;
/* falls through */
case DEF:
statements;
break;
case XYZ:
statements;
break;
default:
statements;
break;
}
try {
statements;
} catch (ExceptionClass e) {
statements;
}
try {
statements;
} catch (ExceptionClass e) {
statements;
} finally {
statements;
}
while (true) {
...
}
a += c + d;
a = (a + b) / (c * d);
while (d++ = s++) {
n++;
}
printSize("size is " + foo + "\n");
for (expr1; expr2; expr3)
myMethod((byte) aNum, (Object) x);
myMethod((int) (cp + 5), ((int) (i + 3)) + 1);
标识符类型
|
命名规则
|
例子
|
包
(Packages)
|
一个唯一包名的前缀总是全部小写的
ASCII字母并且是一个顶级域名,通常是com,edu,gov,mil,net,org,或1981年ISO 3166标准所指定的标识国家的英文双字符代码。包名的后续部分根据不同机构各自内部的命名规范而不尽相同。这类命名规范可能以特定目录名的组成来区分部门(department),项目(project),机器(machine),或注册名(login names)。
|
com.sun.eng com.apple.quicktime.v2 edu.cmu.cs.bovik.cheese |
类
(Classes)
|
命名规则:类名是个一名词,采用大小写混合的方式,每个单词的首字母大写。尽量使你的类名简洁而富于描述。使用完整单词,避免缩写词
(除非该缩写词被更广泛使用,像URL,HTML)
|
class Raster; class ImageSprite; |
接口
(Interfaces)
|
命名规则:大小写规则与类名相似
|
interface RasterDelegate; interface Storing; |
方法
(Methods)
|
方法名是一个动词,采用大小写混合的方式,第一个单词的首字母小写,其后单词的首字母大写。
|
run(); runFast(); getBackground(); |
变量
(Variables)
|
除了变量名外,所有实例,包括类,类常量,均采用大小写混合的方式,第一个单词的首字母小写,其后单词的首字母大写。变量名不应以下划线或美元符号开头,尽管这在语法上是允许的。
变量名应简短且富于描述。变量名的选用应该易于记忆,即,能够指出其用途。尽量避免单个字符的变量名,除非是一次性的临时变量。临时变量通常被取名为i,j,k,m和n,它们一般用于整型;c,d,e,它们一般用于字符型。 |
char c; int i; float myWidth; |
实例变量
(Instance Variables)
|
大小写规则和变量名相似,除了前面需要一个下划线
|
int _employeeId; String _name; Customer _customer; |
常量
(Constants)
|
类常量和
ANSI常量的声明,应该全部大写,单词间用下划线隔开。(尽量避免ANSI常量,容易引起错误)
|
static final int MIN_WIDTH = 4; static final int MAX_WIDTH = 999; static final int GET_THE_CPU = 1; |
classMethod(); //OK
AClass.classMethod(); //OK
anObject.classMethod(); //AVOID!
fooBar.fChar = barFoo.lchar = 'c'; // AVOID!
if (c++ = d++) { // AVOID! (Java disallows)
...
}
if ((c++ = d++) != 0) {
...
}
d = (a = b + c) + r; // AVOID!
a = b + c;
d = a + r;
if (a == b && c == d) // AVOID!
if ((a == b) && (c == d))// RIGHT
if (booleanExpression) {
return true;
} else {
return false;
}
return booleanExpression;
if (condition) {
return x;
}
return y;
return (condition ? x : y);
(x >= 0) ? x : -x;
/*
* @(#)Blah.java 1.82 99/03/18
*
* Copyright (c) 1994-1999 Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information").You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*/
package java.blah;
import java.blah.blahdy.BlahBlah;
/**
* Class description goes here.
*
* @version 1.82 18 Mar 1999
* @author Firstname Lastname
*/
public class Blah extends SomeClass {
/* A class implementation comment can go here. */
/** classVar1 documentation comment */
public static int classVar1;
/**
* classVar2 documentation comment that happens to be
* more than one line long
*/
private static Object classVar2;
/** instanceVar1 documentation comment */
public Object instanceVar1;
/** instanceVar2 documentation comment */
protected int instanceVar2;
/** instanceVar3 documentation comment */
private Object[] instanceVar3;
/**
* ...constructor Blah documentation comment...
*/
public Blah() {
// ...implementation goes here...
}
/**
* ...method doSomething documentation comment...
*/
public void doSomething() {
// ...implementation goes here...
}
/**
* ...method doSomethingElse documentation comment...
* @param someParam description
*/
public void doSomethingElse(Object someParam) {
// ...implementation goes here...
}