android的编程规范,Android进阶之路——安卓编程规范

开源项目必须添加文件注释,非开源项目建议添加,例如:

/*

* @(#): Document.java

* @project:IndentObjectNatation

* @version: v1.1

* @copyright: Copyright (C) 2013-2014 The Emerald Education

* @description:

* This file is a part of Indent Object Notation project.

*

* @modify:

* ---- No.1 Modified By Mr. Tang At 2014-05-06 11:32 Based On 1.0 ----

* Create this file.

* ---- No.2 Modified By Mr. Zhang At 2014-05-06 11:32 Based On 1.0 ----

* Make the class Document extend from the class Node.

*/

类定义一般需要写类注释,接口一般需要写接口注释,如果没有文件注释,则需要在类注释和接口注释中标出作者,例如:

/**

* Root of the ION tree, provides the access to the document's data.

*

* Subclass of {@link Node}, Since elements, comments, etc. cannot exist

* outside the context of a Document, the Document also contains the

* factory methods needed to create these objects.

*

*

* @author Mr. Zhang

*/

class Document {}

成员变量、静态变量、常量等添加属性注释,例如:

/** This view is invisible. */

public static final int INVISIBLE = 0x00000004;

关联性较大的多个成员变量等可以共用同一条注释,例如:

/** The width and height of View. */

private int mWidth, mHeight;

public和protected方法必须添加方法注释,default和private方法建议添加方法注释,例如:

/**

* Writes {@code count} characters starting at {@code offset} in {@code buf}

* to the target.

*

* @param buf

* the non-null character array to write.

* @param offset

* the index of the first character in {@code buf} to write.

* @param count

* the maximum number of characters to write.

* @return {@code true} if success, otherwise {@code false}

* @throws IndexOutOfBoundsException

* if {@code offset < 0} or {@code count < 0}, or if {@code

* offset + count} is greater than the size of {@code buf}.

* @throws IOException

* if this writer is closed or another I/O error occurs.

*/

public abstract boolean write(char[] buf, int offset, int count) throws IndexOutOfBoundsException, IOException;

若覆盖基类的方法,则可以不写方法注释,但必须用@Override标出,例如:

@Override

protected void onCreate(Bundle savedInstanceState) {}

不建议继续使用的方法用@Deprecated标出

switch……case的每个条件一般添加简短说明,例如:

switch (type) {

case 1:// Android apps

break;

case 2:// Android games

break;

case 3:// iOS apps

break;

default:// Not a valid package

break;

}

如果if的条件大于2个,则必须写注释,例如:

if (isBluetooth // If Bluetooth network is on

|| isWifi // If WLAN network is on

|| is3g // if 3g network is on

) {}

对于未完成的方法,使用TODO加以标记,例如:

void write(byte[] buf, File file) {

// TODO: Write buf to file

}

若功能已完成,但存在效率等潜在问题时,使用XXX加以标记,例如:

void parseXML(File file) {

// XXX: Maybe SAX is better

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document doc = builder.parse(file);

}

若代码存在严重问题或仅用于调试,使用FIXME加以标记(注:存在FIXME标记的代码不能作为正式版发布)

boolean login(String name, String pwd) {

//FIXME: Remove this line before publishing

System.out.println("name=" + name + ", password=" + pwd);

if (users.containsKey(name) && users.get(name).equals(pwd))

return true;

return false;

}

如果for、while等代码块过长,可以在结尾处标记循环变量,例如:

for (int position = 0; position < 10; position++) {

……

}// end for: position

while(mRun){

……

}// end while: mRun

leo差不多就介绍这些了,欢迎补充!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值