Android代码规范

发布时间:2020-09-29 03:22:36 作者:杨光成
来源:网络 阅读:274

Android代码规范  ——转发请表明出处《IT蓝豹》:http://itlanbao.com/preview.aspx#1,0


[-]

  1. 一Import的次序

  2. 二缩进Indentation

    1. 总则

    2. 示例代码

    3. 规则说明

  3. 三大括号Braces的位置

    1.  

    2. 示例代码

    3. 规则说明

  4. 四空格White Space

    1. 声明

    2. 申请

    3. 初始化体

    4. 数组元素的访问

    5. 函数调用

    6. 赋值

    7. 操作数

    8. 加括号的表达式

    9. 类型转换

    10. 三元条件表达式

    11. 程序块

    12. if else语句

    13. for语句

    14. switch语句

    15. while和dowhile语句

    16. 同步synchronized语句

    17. catch语句

    18. assert语句

    19. return语句

    20. throw语句

    21. 临时变量

    22. 构造体

    23. 方法

    24. 标号

    25. 注解Annotation

    26. 枚举Enumtypes

    27. 注解类型Annotationtypes

    28. 声明

    29. 控制语句

    30. 表达式

    31. 数组

    32. 泛型

  5. 五空白行BlankLines

    1. 示例代码

    2. 规则说明

    3. 七控制语句ControlStatements

    4. 八换行LineWrapping

    5. 九注释Comments

    6. 十Android中XML文件的格式化

    7. 总结

    8. 插入新行

    9. 数组初始化

    10. 空的语句

    11. 注解

    12. 示例代码

    13. 规则说明

    14. 总则

    15. 注解Annotation

    16. 类声明

    17. 构造体声明

    18. 方法声明

    19. 枚举声明

    20. 函数调用

    21. 表达式

    22. 语句

    23. 编译单元之间的空白行

    24. 类内部的空白行

    25. 示例代码

    26. 规则说明

    27. 六插入新行NewLines

 

Google对Android的编程规范在Code Style Guidelines for Contributors中做了描述,并在Android源码中release了import和Java的配置文件android.importorder与android-formatting.xml。本文分析这些配置文件在Eclipse环境下格式化Android编码规范都做了什么,在Java和XML文件中如何具体体现。

Android源码目录<android_src_root>/development/ide/eclipse/下有文件android.importorder和android-formatting.xml,可以在Eclipse中导入import次序及Java编码风格:

1)?????打开Window > Preferences > Java > Code Style;

2)?????在Organizer Imports中点击Imports,选择android.importorder导入;

3)?????在Formatter中点击Imports,选择android-formatting.xml导入。

下面讲解这些配置都做了什么,在代码中如何具体体现的。

一、Import的次序

Google推荐的AndroidJava文件开头import的次序(按照先后)是:

  1. com

  2. org

  3. android

  4. java

  5. javax

排列原则:

 

 

二、缩进(Indentation)

2.1 总则

缩进只用空格,不用制表符(TAB)。缩进用4个空格,按下TAB键用4个空格代替。

?

2.2 示例代码

?

[java]?view plaincopy

  1. /**?

  2. ?*?Indentation?

  3. ?*/??

  4. class?Example?{??

  5. ????int[]?myArray?=?{??

  6. ????????????1,?2,?3,?4,?5,?6??

  7. ????};??

  8. ???

  9. ????int?theInt?=?1;??

  10. ???

  11. ????String?someString?=?"Hello";??

  12. ???

  13. ????double?aDouble?=?3.0;??

  14. ???

  15. ????void?foo(int?a,?int?b,?int?c,?int?d,?int?e,?int?f)?{??

  16. ????????switch?(a)?{??

  17. ????????????case?0:??

  18. ????????????????Other.doFoo();??

  19. ????????????????break;??

  20. ????????????default:??

  21. ????????????????Other.doBaz();??

  22. ????????}??

  23. ????}??

  24. ???

  25. ????void?bar(List?v)?{??

  26. ????????for?(int?i?=?0;?i?<10;?i++)?{??

  27. ????????????v.add(new?Integer(i));??

  28. ????????}??

  29. ????}??

  30. }??

  31. ???

  32. enum?MyEnum?{??

  33. ????UNDEFINED(0)?{??

  34. ????????void?foo()?{??

  35. ????????}??

  36. ????}??

  37. }??

  38. ???

  39. @interface?MyAnnotation?{??

  40. ????int?count()?default?1;??

  41. }??

  42. ???

 

2.3 规则说明

 

1)????? 域不用对齐

? ? ? ??若对齐的话,则myArray,theInt, someString和aDouble都在同一列上对齐。

2)?????类体内部的声明全都缩进

? ? ? ??Class Example内的定义[#5 ~ #29]相对class?Example[#4]都有缩进

3)?????枚举的声明要缩进

? ? ? ??UNDEFINED(0) [#33]前面有缩进

4)?????枚举内的常量要缩进

? ? ? ??void?foo() [#34]前面有缩进

5)?注解的声明要缩进

? ? ? ??int?count()[#39]前面有缩进

6)?????方法/构造体内的语句要缩进

? ? ? ??方法foo和bar内的语句[#16 ~ #22, #26 ~ #28]都有缩进

7)?????程序块内的语句要缩进

? ? ? ??for循环内的v.add(new?Integer(i))[#27]有缩进

8)?????switch内的语句要缩进

? ? ? ? switch内的语句[#17 ~ #21]相对switch有缩进

9)?????case内的语句要缩进

? ? ? ??Other.doFoo()[#18]相对于case;Other.doBaz()[#21]相对于default都有缩进

10)??break语句要缩进

? ? ? ??break[#19]相对于case有缩进

11)??空白行不用缩进

? ? ? ??域和方法之间的空白行[#8, #10, #12, #14, #24]是没有缩进的

三、大括号(Braces)的位置

 

3.1 示例代码

[java]?view plaincopy

  1. /**?

  2. ?*?Braces?

  3. ?*/??

  4. interface?Empty?{??

  5. }??

  6. ???

  7. enum?MyEnum?{??

  8. ????UNDEFINED(0)?{??

  9. ????????void?foo()?{??

  10. ????????}??

  11. ????}??

  12. }??

  13. ???

  14. @interfaceSomeAnnotationType?{??

  15. }??

  16. ???

  17. class?Example?{??

  18. ????SomeClass?fField?=?new?SomeClass()?{??

  19. ????};??

  20. ???

  21. ????int[]?myArray?=?{??

  22. ????????????1,?2,?3,?4,?5,?6??

  23. ????};??

  24. ???

  25. ????int[]?emptyArray?=?new?int[]?{};??

  26. ???

  27. ????Example()?{??

  28. ????}??

  29. ???

  30. ????void?bar(int?p)?{??

  31. ????????for?(int?i?=?0;?i?<10;?i++)?{??

  32. ????????}??

  33. ????????switch?(p)?{??

  34. ????????????case?0:??

  35. ????????????????fField.set(0);??

  36. ????????????????break;??

  37. ????????????case?1:?{??

  38. ????????????????break;??

  39. ????????????}??

  40. ????????????default:??

  41. ????????????????fField.reset();??

  42. ????????}??

  43. ????}??

  44. }??

  45. ???


?

3.2? 规则说明

1)类或接口的声明跟左大括号在同一行上

#4 Empty以及 #17 Example后面{的位置

2)?????匿名类的声明跟左大括号在同一行上

? ? ? ??#18 SomeClass后面{的位置

3)?????构造体的声明跟左大括号在同一行上

? ? ? ??#27 Example()后面{的位置

4)?????方法的声明跟左大括号在同一行上

? ? ? ??#9 foo和#30 bar后面{的位置

5)?????枚举的声明跟左大括号在同一行上

? ? ? ??#7 MyEnum 后面{的位置

6)?????枚举常量体跟左大括号在同一行上

? ? ? ??#8 UNDEFINED(0) 后面{的位置

7)?????注解类型的声明跟左大括号在同一行上

? ? ? ??#14 SomeAnnotationType后面{的位置

8)?????程序块跟左大括号在同一行上

? ? ? ??#31 for后面{的位置

9)?????case语句中的程序块跟左大括号在同一行上

? ? ? ??#37 case 1后面{的位置

10)??switch语句跟左大括号在同一行上

? ? ? ??#33 switch后面{的位置

11)??数组的初始化常量跟左大括号在同一行上

? ? ? ? #21和#25 {的位置

?

?

四、空格(White Space)

?

4.1 声明

?

4.1.1 类

?

[java]?view plaincopy

  1. class?MyClass?implements?I0,?I1,?I2?{??

  2. }??

  3. ???

  4. AnonClass?=?new?AnonClass()?{??

  5. ????void?foo(Some?s)?{??

  6. ????}??

  7. };??

·????????类的左大括号的前面加空格;

·????????匿名类的左大括号的前面加空格;

·????????implements语句中逗号的前面,不加空格;

·????????implements语句中逗号的后面,加上空格;

?

4.1.2 域

?

[java]?view plaincopy

  1. int?a?=?0,?b?=?1,?c=?2,?d?=?3;??

?

·????????多个域声明中逗号的前面,不加空格;

·????????多个域声明中逗号的后面,加上空格。

?

4.1.3 临时变量

?

[java]?view plaincopy

  1. int?a?=?0,?b?=?1,?c=?2,?d?=?3;??

?

·????????多个临时变量声明中逗号的前面,不加空格;

·????????多个临时变量声明中逗号的后面,加上空格;

?

4.1.4 构造体

[java]?view plaincopy

  1. MyClass()?throws?E0,?E1?{??

  2. ????this(0,?0,?0);??

  3. }??

  4. ???

  5. MyClass(int?x,?int?y,?int?z)?throws?E0,?E1?{??

  6. ????super(x,?y,?z,?true);??

  7. }??


·????????左小括号的前面,不加空格;

·????????左小括号的后面,不加空格;

·????????右小括号的前面,不加空格;

·????????小括号内为空,则它们之间不加空格;

·????????左大括号前面,加上空格;

·????????参数列表中逗号之前,不加空格;

·????????参数列表中逗号之后,加上空格;

·????????throws语句中逗号之前,不加空格;

·????????throws语句中逗号之后,加上空格。

?

4.1.5 方法

?

[java]?view plaincopy

  1. void?foo()?throws?E0,?E1?{??

  2. };??

  3. ???

  4. void?bar(int?x,?int?y)?throws?E0,?E1?{??

  5. }??

  6. ???

  7. void?format(Strings,?Object...?args)?{??

  8. }??

·????????左小括号的前面,不加空格;

·????????左小括号的后面,不加空格;

·????????右小括号的前面,不加空格;

·????????小括号内为空,则它们之间不加空格;

·????????左大括号前面,加上空格;

·????????参数列表中逗号之前,不加空格;

·????????参数列表中逗号之后,加上空格;

·????????可变参数列表省略号之前,不加空格;

·????????可变参数列表省略号之后,加上空格;

·????????throws语句中逗号之前,不加空格;

·????????throws语句中逗号之后,加上空格。

?

4.1.6 标号

?

[java]?view plaincopy

  1. label:?for?(int?i?=?0;?i?<?list.length;?i++)?{??

  2. ????for?(int?j?=?0;?j?<?list[i].length;?j++)??

  3. ????????continue?label;??

  4. }??

·????????冒号之前,不加空格;

·????????冒号之后,加上空格

?

4.1.7 注解/Annotation

?

[java]?view plaincopy

  1. @Annot(x?=?23,?y?=?-3)??

  2. public?class?A?{??

  3. }??

·????????‘@’之后,不加空格;

·????????左小括号的前面,不加空格;

·????????左小括号的后面,不加空格;

·????????逗号前面,不加空格;

·????????逗号后面,加上空格;

·????????右小括号的前面,不加空格

?

4.1.8 枚举/Enumtypes

?

[java]?view plaincopy

  1. enum?MyEnum?{??

  2. ????GREEN(0,?1),?RED()?{??

  3. ????????void?process()?{??

  4. ????????}??

  5. ????}??

  6. }??

·????????声明中左大括号的前面[#1],加上空格;

·????????常量之间的逗号[#2 RED前]前面,不加空格;

·????????常量之间的逗号[#2 RED前]后面,加上空格;

·????????常量参数的左小括号[#2 GREEN后]前面,不加空格;

·????????常量参数的左小括号[#2 GREEN后]后面,不加空格;

·????????常量参数的小括号[#2 RED后]中间为空,括号之间不加空格;

·????????常量参数之间的逗号[#2 GREEN()里面]前面,不加空格;

·????????常量参数之间的逗号[#2 GREEN()里面]后面,加上空格;

·????????常量参数的右小括号[#2 GREEN()后]前面,不加空格;

·????????常量体左大括号[#2 RED后]前面,加上空格。

?

4.1.9 注解类型/Annotationtypes

?

[java]?view plaincopy

  1. @interface?MyAnnotation?{??

  2. ????String?value();??

  3. }??

  4. ???

  5. @interface?OtherAnnotation?{??

  6. }??

·????????‘@’之前,不加空格;

·????????‘@’之后,不加空格

·????????左大括号的前面,加上空格;

·????????注解类型成员的左小括号的前面,不加空格;

·????????注解类型成员的小括号的之间,不加空格;

?

4.2 控制语句

?

4.2.1 程序块

?

[java]?view plaincopy

  1. if?(true)?{??

  2. ????return?1;??

  3. }?else?{??

  4. ????return?2;??

  5. }??

·????????左大括号前面,加上空格;

·????????右大括号后面,加上空格。

?

4.2.2 if else语句

?

[java]?view plaincopy

  1. if?(condition)?{??

  2. ????return?foo;??

  3. }?else?{??

  4. ????return?bar;??

  5. }??

·????????左小括号前加上空格;

·????????左小括号后不加空格;

·????????右小括号前不加空格【左大括号前的空格是规则#4.2.1】

?

4.2.3 for语句

?

[java]?view plaincopy

  1. for?(int?i?=?0,?j?=?array.length;?i?<?array.length;?i++,?j--)?{??

  2. }??

  3. for?(String?s?:?names)?{??

  4. }??

·????????左小括号前加上空格;

·????????左小括号后不加空格;

·????????右小括号前不加空格【左大括号前的空格是规则#4.2.1】

·????????初始化语句的逗号前不加空格;

·????????初始化语句的逗号后加上空格

·????????增量语句的逗号前不加空格;

·????????增量语句的逗号后加上空格

·????????语句之间的分号前不加空格;

·????????语句之间的分号后加上空格;

·????????冒号前面加上空格;

·????????冒号后面加上空格。

?

4.2.4 switch语句

?

[java]?view plaincopy

  1. switch?(number)?{??

  2. ????case?RED:??

  3. ????????return?GREEN;??

  4. ????case?GREEN:??

  5. ????????return?BLUE;??

  6. ????case?BLUE:??

  7. ????????return?RED;??

  8. ????default:??

  9. ????????return?BLACK;??

  10. }??

· ? ? ? ?case和default的冒号(‘:’)前不加空格;

·????????左括号(‘(’)和左大括号(‘{’)前都加上空格;

·????????左括号(‘(’)后和右括号(‘)’)前都不加空格。

?

4.2.5 while和dowhile语句

?

[java]?view plaincopy

  1. while?(condition)?{??

  2. }??

  3. ;??

  4. do?{??

  5. }?while?(condition);??

·????????左括号前加上空格;

·????????左括号后不加空格;

·????????右括号前不加空格【#1左大括号前的空格是规则#4.2.1】

?

4.2.6 同步(synchronized)语句

?

[java]?view plaincopy

  1. synchronized?(list)?{??

  2. ????list.add(element);??

  3. }??

·????????左括号前加上空格;

·????????左括号后不加空格;

·????????右括号前不加空格【左大括号前的空格是规则#4.2.1】

?

4.2.7 catch语句

?

[java]?view plaincopy

  1. try?{??

  2. ????number?=?Integer.parseInt(value);??

  3. }?catch?(NumberFormatException?e)?{??

  4. }??

·????????左括号前加上空格;

·????????左括号后不加空格;

·????????右括号前不加空格【左大括号前的空格是规则#4.2.1】

?

4.2.8 assert语句

?

[java]?view plaincopy

  1. assert?condition?:?reportError();??

?

冒号前后都加上空格

?

4.2.9 return语句

?

[java]?view plaincopy

  1. return?(o);??

?

括号表达式前加上空格

?

4.2.10 throw语句

?

[java]?view plaincopy

  1. throw?(e);??

?

括号表达式前加上空格

?

4.3 表达式

?

4.2.1 函数调用

?

[java]?view plaincopy

  1. foo();??

  2. bar(x,?y);??

  3. ???

  4. String?str?=?new?String();??

  5. Point?point?=?new?Point(x,?y);??

  6. ???

  7. MyClass()?throws?E0,?E1?{??

  8. ????this(0,?0,?0);??

  9. }??

  10. ???

  11. MyClass(int?x,?int?y,?int?z)?throws?E0,?E1?{??

  12. ????super(x,?y,?z,?true);??

  13. }??

·????????左括号的前后都不加空格;

·????????右括号前不加空格;

·????????空的参数的左右括号之间不加空格;

·????????方法调用时多个参数之间分割的逗号前面不加空格,逗号后面加空格;

·????????对象申请时多个参数之间分割的逗号前面不加空格,逗号后面加空格;

·????????显示调用构造函数时多个参数之间分割的逗号前面不加空格,逗号后面加空格;

?

4.3.2 赋值

?

[java]?view plaincopy

  1. List?list?=?new?ArrayList();??

  2. int?a?=?-4?+?-9;??

  3. b?=?a++?/?--number;??

  4. c?+=?4;??

  5. boolean?value?=?true?&&?false;??

赋值操作(=)前后都加上空格。【注意:‘+=’是一个操作数】

?

4.3.3 操作数

?

[java]?view plaincopy

  1. List?list?=?new?ArrayList();??

  2. int?a?=?-4?+?-9;??

  3. b?=?a++?/?--number;??

  4. c?+=?4;??

  5. boolean?value?=?true?&&?false;??

·????????二元操作(#2的‘+’;#3的‘/’;#5的‘&&’)前后都加上空格;

·????????一元操作(#2 ‘4’和‘9’前面的‘-’)前后都不加空格【示例中‘-’前的空格不是这个规则里的】;

·????????前置操作的(#3的‘--number’)前后都不加空格【示例中‘--’前的空格不是这个规则里的】;

·????????后置操作的(#3的‘a++’)前后都不加空格【示例中‘++’后的空格不是这个规则里的】。

?

4.3.4 加括号的表达式

?

[java]?view plaincopy

  1. result?=?(a?*?(b?+?c?+?d)?*?(e?+?f));??

?

左括号前,左括号后和右括号前都不加空格【示例中左括号前的空格不是这个规则里的】

?

4.3.5 类型转换

?

[java]?view plaincopy

  1. String?s?=?((String)?object);??

?

·????????右括号后加上空格;

·????????左括号后和右括号前都不加空格。

?

4.3.6 三元条件表达式

?

[java]?view plaincopy

  1. String?value?=?condition???TRUE?:?FALSE;??

?

问号前,问号后,冒号前,冒号后都要加上空格

?

4.4 数组

?

?

[java]?view plaincopy

  1. int[]?array0?=?new?int[]?{};??

  2. int[]?array1?=?new?int[]?{??

  3. ????????1,?2,?3??

  4. };??

  5. int[]?array2?=?new?int[3];??

  6. ???

  7. array[i].foo();??

?

4.4.1 声明

·????????左中括号前不加空格;

·????????左右中括号中间不加空格

示例中:arrayX前的‘[]’

?

4.4.2 申请

·????????左中括号前后都不加空格;

·????????右中括号前不加空格;

·????????空的左右中括号中间不加空格;

示例中:等号‘=’后面的‘[]’

?

4.4.3 初始化体

·????????左大括号前后都加上空格;

·????????右大括号前加上空格;

·????????逗号前不加空格;

·????????逗号后加上空格;

·????????空的大括号中间不加空格

?

4.4.4 数组元素的访问

·????????左中括号前后都不加空格;

·????????右中括号前面不加空格

?

4.5 泛型

?

[java]?view plaincopy

  1. Map<String,?Element>?map?=?newHashMap<String,?Element>();??

  2. ???

  3. x.<String,?Element>?foo();??

  4. ???

  5. classMyGenericType<S,?T?extends?Element?&?List>?{??

  6. }??

  7. ???

  8. Map<X<?>,?Y<??extendsK,???super?V>>?t;??

?

?

五、空白行(BlankLines)

?

5.1 示例代码

?

[java]?view plaincopy

  1. /**?

  2. ?*?Blank?Lines?

  3. ?*/??

  4. ???

  5. package?foo.bar.baz;??

  6. ???

  7. import?java.util.List;??

  8. import?java.util.Vector;??

  9. ???

  10. import?java.net.Socket;??

  11. ???

  12. public?class?Another?{??

  13. }??

  14. ???

  15. public?class?Example?{??

  16. ????public?static?class?Pair?{??

  17. ????????public?String?first;??

  18. ???

  19. ????????public?String?second;??

  20. ????????//?Between?here...??

  21. ???

  22. ????????//?...and?here?are?10?blank?lines??

  23. ????};??

  24. ???

  25. ????private?LinkedList?fList;??

  26. ???

  27. ????public?int?counter;??

  28. ???

  29. ????publicExample(LinkedList?list)?{??

  30. ????????fList?=?list;??

  31. ????????counter?=?0;??

  32. ????}??

  33. ???

  34. ????public?void?push(Pair?p)?{??

  35. ????????fList.add(p);??

  36. ????????++counter;??

  37. ????}??

  38. ???

  39. ????public?Object?pop()?{??

  40. ????????--counter;??

  41. ????????return?(Pair)fList.getLast();??

  42. ????}??

  43. }??

  44. ???


?

5.2 规则说明

?

5.2.1 编译单元之间的空白行

1)?????包声明之前有一空白行[#4]

2)?????包声明之后有一空白行[#6]

3)?????import声明之前有一空白行[#6]

4)?????import各个分组之间有一空白行[#9]

5)?????import声明之后有一空白行[#11]

6)?????类声明之间有一空白行[#14]

?

5.2.2 类内部的空白行

1)?????第一个声明之间无空白行[#15 & #16之间]

2)?????相同分类声明之前有一空白行[#24, #28]

3)?????成员类声明之前有一空白行

4)?????域声明之前有一空白行[#18, #24, #26]

5)????? 方法声明之前有一空白行[#28,#33, #38]??????????

6)????? 方法内的开始处没有空白行[#29和#30之间;#34与#35之间;#39与#40之间]

?

?

六、插入新行(NewLines)

?

6.1 示例代码

?

[java]?view plaincopy

  1. /**?

  2. ?*?New?Lines?

  3. ?*/??

  4. public?class?Empty?{??

  5. }??

  6. ???

  7. class?Example?{??

  8. ????static?int[]?fArray?=?{??

  9. ????????????1,?2,?3,?4,?5??

  10. ????};??

  11. ???

  12. ????Listener?fListener?=?new?Listener()?{??

  13. ????};??

  14. ???

  15. ????@Deprecated??

  16. ????@Override??

  17. ????public?void?bar(@SuppressWarnings("unused")?int?i)?{??

  18. ????????@SuppressWarnings("unused")??

  19. ????????int?k;??

  20. ????}??

  21. ???

  22. ????void?foo()?{??

  23. ????????;??

  24. ????????;??

  25. ????????label:?do?{??

  26. ????????}?while?(false);??

  27. ????????for?(;;)?{??

  28. ????????}??

  29. ????}??

  30. }??

  31. ???

  32. enum?MyEnum?{??

  33. ????UNDEFINED(0)?{??

  34. ????}??

  35. }??

  36. ???

  37. enum?EmptyEnum?{??

  38. }??

  39. ???

  40. @interface?EmptyAnnotation{??

  41. }??

  42. ???

?

6.2 规则说明

?

6.2.1 插入新行

1)?????类体内为空,插入新行[#5是另起的一行]

2)?????匿名类体内为空,插入新行[#13是另起的一行]

3)?????方法内为空,插入新行

4)?????空的程序块,插入新行[#26是另起的一行;#28是另起的一行]

5)?????标号后面不插入新行[#25 do与label在同一行]

6)?????在空的枚举声明中,插入新行[#38是另起的一行]

7)?????在空的枚举常量体中,插入新行[#34是另起的一行]

8)?????在空的注解体中,插入新行[#41是另起的一行]

9)?????在文件结尾,插入新行[#42是另起的一行]

?

6.2.2 数组初始化

1)?????数组初始化体的左大括号后,插入新行[#9是另起的一行]

2)?????数组初始化体的右大括号前,插入新行[#10是另起的一行]

?

6.2.3 空的语句

空的语句放在新行上[#24是另起的一行]

?

6.2.4 注解

1)?????对成员的注解之后,插入新行[#16 & #17都是另起的一行]

2)?????对参数的注解之后,不插入新行[#17 int i与@SuppressWarnings("unused")在同一行]

3)?????对临时变量的注解之后,插入新行[#19是另起的一行]

?

?

七、控制语句(ControlStatements)

?

7.1 示例代码

?

[java]?view plaincopy

  1. /**?

  2. ?*?If...else?

  3. ?*/??

  4. class?Example?{??

  5. ????void?bar()?{??

  6. ????????do?{??

  7. ????????}?while?(true);??

  8. ????????try?{??

  9. ????????}?catch?(Exception?e)?{??

  10. ????????}?finally?{??

  11. ????????}??

  12. ????}??

  13. ???

  14. ????void?foo2()?{??

  15. ????????if?(true)?{??

  16. ????????????return;??

  17. ????????}??

  18. ????????if?(true)?{??

  19. ????????????return;??

  20. ????????}?else?if?(false)?{??

  21. ????????????return;??

  22. ????????}?else?{??

  23. ????????????return;??

  24. ????????}??

  25. ????}??

  26. ???

  27. ????void?foo(int?state)?{??

  28. ????????if?(true)??

  29. ????????????return;??

  30. ????????if?(true)??

  31. ????????????return;??

  32. ????????else?if?(false)??

  33. ????????????return;??

  34. ????????else??

  35. ????????????return;??

  36. ????}??

  37. }??

  38. ???

?

7.2 规则说明

?

1)?????if语句的else之前,不插入新行[#20& #22的else与‘}’在同一行]

2)?????try语句的catch之前,不插入新行[#9的catch与‘}’在同一行]

3)?????try语句的finally之前,不插入新行[#10的finally与‘}’在同一行]

4)?????do语句的while之前,不插入新行[#7的while与‘}’在同一行]

5)?????#29的‘then’语句与#28的if在不同行,#31的‘then’语句与#30的if在不同行;

6)?????#35的else语句与#34的else在不同行;

7)?????#20和#32的else if中‘else与‘if’在同一行;

8)?????‘return’或‘throw’语句不需要在一行上[#16与#15在两行上]

?

?

八、换行(LineWrapping)

?

8.1 总则

?

8.2 注解(Annotation)

[java]?view plaincopy

  1. /**?

  2. ?*?Element-value?pairs?

  3. ?*/??

  4. @MyAnnotation(value1?=?"this?isan?example",?value2?=?"of?an?annotation",?value3?=?"withseveral?arguments",?value4?=?"by?Haili?TIAN?(haili.tian@gmail.com)")??

  5. class?Example?{??

  6. }??

注解不换行:value1、value2、value3和value4都在同一行上。

?

8.3 类声明

?

[java]?view plaincopy

  1. /**?

  2. ?*?'extends'?clause?

  3. ?*/??

  4. class?Example?extends??

  5. ????????OtherClass?{??

  6. }??

  7. ???

  8. /**?

  9. ?*?'implements'?clause?

  10. ?*/??

  11. class?Example?implements?I1,??

  12. ????????I2,?I3?{??

  13. }??

  14. ???

?

?

8.4 构造体声明

?

[java]?view plaincopy

  1. /**?

  2. ?*?Parameters?

  3. ?*/??

  4. class?Example?{??

  5. ????Example(int?arg1,?int?arg2,??

  6. ????????????int?arg3,?int?arg4,??

  7. ????????????int?arg5,?int?arg6)?{??

  8. ????????this();??

  9. ????}??

  10. ???

  11. ????Example()?{??

  12. ????}??

  13. }??

  14. ???

  15. /**?

  16. ?*?'throws'?clause?

  17. ?*/??

  18. class?Example?{??

  19. ????Example()?throws?FirstException,??

  20. ????????????SecondException,??

  21. ????????????ThirdException?{??

  22. ????????returnOther.doSomething();??

  23. ????}??

  24. }??

  25. ???

?

?

8.5 方法声明

?

[java]?view plaincopy

  1. /**?

  2. ?*?Declaration?

  3. ?*/??

  4. class?Example?{??

  5. ????public?final?synchronizedjava.lang.String?a_method_with_a_long_name()?{??

  6. ????}??

  7. }??

  8. ???

  9. /**?

  10. ?*?Parameters?

  11. ?*/??

  12. class?Example?{??

  13. ????void?foo(int?arg1,?int?arg2,??

  14. ????????????int?arg3,?int?arg4,??

  15. ????????????int?arg5,?int?arg6)?{??

  16. ????}??

  17. }??

  18. ???

  19. /**?

  20. ?*?'throws'?clause?

  21. ?*/??

  22. class?Example?{??

  23. ????int?foo()?throws?FirstException,??

  24. ????????????SecondException,??

  25. ????????????ThirdException?{??

  26. ????????returnOther.doSomething();??

  27. ????}??

  28. }??

  29. ???

?

?

8.6 枚举声明

?

[java]?view plaincopy

  1. /**?

  2. ?*?Constants?

  3. ?*/??

  4. enum?Example?{??

  5. ????CANCELLED,?RUNNING,?WAITING,?FINISHED??

  6. }??

  7. ???

  8. enum?Example?{??

  9. ????GREEN(0,?255,?0),?RED(??

  10. ????????????255,?0,?0)??

  11. }??

  12. ???

  13. /**?

  14. ?*?'implements'?clause?

  15. ?*/??

  16. enum?Example?implements?A,?B,??

  17. ????????C?{??

  18. }??

  19. ???

  20. /**?

  21. ?*?Constant?arguments?

  22. ?*/??

  23. enum?Example?{??

  24. ????GREEN(0,?255,?0),?RED(??

  25. ????????????255,?0,?0)??

  26. }??

  27. ???

?

?

8.7 函数调用

?

[java]?view plaincopy

  1. /**?

  2. ?*?Arguments?

  3. ?*/??

  4. class?Example?{??

  5. ????void?foo()?{??

  6. ????????Other.bar(??

  7. ????????????????100,??

  8. ????????????????nested(200,?300,?400,??

  9. ????????????????????????500,?600,?700,??

  10. ????????????????????????800,?900));??

  11. ????}??

  12. }??

  13. ???

  14. /**?

  15. ?*?Qualified?invocations?

  16. ?*/??

  17. class?Example?{??

  18. ????int?foo(Some?a)?{??

  19. ????????return?a.getFirst();??

  20. ????}??

  21. }??

  22. ???

  23. /**?

  24. ?*?Explicit?constructor?invocations?

  25. ?*/??

  26. class?Example?extends?AnotherClass?{??

  27. ????Example()?{??

  28. ????????super(100,?200,?300,400,?500,??

  29. ????????????????600,?700);??

  30. ????}??

  31. }??

  32. ???

  33. /**?

  34. ?*?Object?allocation?arguments?

  35. ?*/??

  36. class?Example?{??

  37. ????SomeClass?foo()?{??

  38. ????????return?new?SomeClass(100,200,??

  39. ????????????????300,?400,?500,?600,??

  40. ????????????????700,?800,?900);??

  41. ????}??

  42. }??

  43. ???

  44. /**?

  45. ?*?Qualified?object?allocation?arguments?

  46. ?*/??

  47. class?Example?{??

  48. ????SomeClass?foo()?{??

  49. ????????return?SomeOtherClass.new?SomeClass(??

  50. ????????????????100,?200,?300,?400,?500);??

  51. ????}??

  52. }??

  53. ???

?

?

8.8 表达式

?

[java]?view plaincopy

  1. /**?

  2. ?*?Binary?expressions?

  3. ?*/??

  4. class?Example?extends?AnotherClass?{??

  5. ????int?foo()?{??

  6. ????????int?sum?=?100?+?200?+?300?+?400??

  7. ????????????????+?500?+?600?+?700?+?800;??

  8. ????????int?product?=?1?*?2?*?3?*?4?*?5??

  9. ????????????????*?6?*?7?*?8?*?9?*?10;??

  10. ????????boolean?val?=?true?&&?false??

  11. ????????????????&&?true?&&?false??

  12. ????????????????&&?true;??

  13. ????????return?product?/?sum;??

  14. ????}??

  15. }??

  16. ???

  17. /**?

  18. ?*?Conditionals?

  19. ?*/??

  20. class?Example?extends?AnotherClass?{??

  21. ????int?Example(boolean?Argument)?{??

  22. ????????return?argument??100000??

  23. ????????????????:?200000;??

  24. ????}??

  25. }??

  26. ???

  27. /**?

  28. ?*?Array?initializers?

  29. ?*/??

  30. class?Example?{??

  31. ????int[]?fArray?=?{??

  32. ????????????1,?2,?3,?4,?5,?6,?7,?8,?9,??

  33. ????????????10,?11,?12??

  34. ????};??

  35. }??

  36. ???

  37. /**?

  38. ?*?Assignments?

  39. ?*/??

  40. class?Example?{??

  41. ????private?static?final?String?string?="TextTextText";??

  42. ???

  43. ????void?foo()?{??

  44. ????????for?(int?i?=?0;?i?<10;?i++)?{??

  45. ????????}??

  46. ????????String?s;??

  47. ????????s?=?"TextTextText";??

  48. ????}??

  49. }??

  50. ???

?

?

8.9 语句

?

[java]?view plaincopy

  1. /**?

  2. ?*?Compact?'if?else'?

  3. ?*/??

  4. class?Example?{??

  5. ????int?foo(int?argument)?{??

  6. ????????if?(argument?==?0)??

  7. ????????????return?0;??

  8. ????????if?(argument?==?1)??

  9. ????????????return?42;??

  10. ????????else??

  11. ????????????return?43;??

  12. ????}??

  13. }??

  14. ???

配置起什么用,没看懂!

?

?

九、注释(Comments)

?

?

[java]?view plaincopy

  1. ??

[java]?view plaincopy

  1. /**?

  2. ?*?An?example?for?comment?formatting.?This?example?is?meant?to?illustrate?the?various?possibilities?offered?by?<i>Haili?TIAN</i>?in?order?to?format?comments.?

  3. ?*/??

  4. ???

  5. package?mypackage;??

  6. ???

  7. /**?

  8. ?*?This?is?the?comment?for?the?example?interface.?

  9. ?*/??

  10. interface?Example?{??

  11. ????//?This?is?a?long?comment?with?white?space?that?should?be?split?in?multiple??

  12. ????//?line?comments?in?case?the?linecomment?formatting?is?enabled??

  13. ????int?foo3();??

  14. ?????

  15. //????void?commented()?{??

  16. //????????System.out.println("indented");??

  17. //????}??

  18. ???

  19. ????//?void?indentedCommented()?{??

  20. ????//?System.out.println("indented");??

  21. ????//?}??

  22. ???

  23. ????/*?block?comment?on?first?column?*/??

  24. ????int?bar();??

  25. ???

  26. ????/*?

  27. ?????*?These?possibilities?include:<ul><li>Formatting?of?header?

  28. ?????*?comments.</li><li>Formatting?of?Javadoc?tags</li></ul>?

  29. ?????*/??

  30. ????int?bar2();?//?This?is?along?comment?that?should?be?split?in?multiple?line??

  31. ????????????????//?comments?in?case?the?linecomment?formatting?is?enabled??

  32. ???

  33. ????/**?

  34. ?????*?The?following?is?some?sample?code?whichillustrates?source?formatting?

  35. ?????*?within?javadoc?comments:?

  36. ?????*?

  37. ?????*?<pre>?

  38. ?????*?public?class?Example?{?

  39. ?????*????final?int?a?=?1;?

  40. ?????*?

  41. ?????*????final?boolean?b?=?true;?

  42. ?????*?}?

  43. ?????*?</pre>?

  44. ?????*?

  45. ?????*?Descriptions?of?parameters?and?returnvalues?are?best?appended?at?end?of?

  46. ?????*?the?javadoc?comment.?

  47. ?????*?

  48. ?????*?@param?a?The?first?parameter.?For?an?optimum?result,?this?should?be?an?

  49. ?????*????????????odd?number?between?0?and?100.?

  50. ?????*?@param?b?The?second?parameter.?

  51. ?????*?@return?The?result?of?the?foo?operation,?usually?within?0?and?1000.?

  52. ?????*/??

  53. ????int?foo(int?a,?int?b);??

  54. }??

?

#37 ~ #43

?

?

#47是插入的行

?

?

#49的odd相对#48的a缩进了4个空格。

?

?

#48和#50的描述与参数在同一行。

?

?

Javadoc注释形式见#33~ #52

?

?

块注释形式见#26~ #29

?

?

#15, #16 和#17是行注释,且‘//’与原程序之间的空格仍旧保持。

?

?

#1, #2和#3的头注释保持不变。

?

?

#23是块注释,与程序有相同的缩进,不是缩进到第一列。

?

?

注:笔者对行注释验证,发现Eclipse中无论如何设置,基本不会改变其行为。

?

?

十、Android中XML文件的格式化

?

前面讲了那么多都是针对Java程序的,Android中有大量的XML文件。对XML的格式也要进行排版格式化。

打开Window> Preferences,可以通过两个地方对XML文件进行格式化:

1)?????XML > XML Files > Editor

对其中的各项设置进行配置

2)?????Android > Editors

对其中的各项设置进行配置

?

?

总结

 本文分析了Android编码风格在Eclipse环境中具体实施,通过对Eclipse环境的配置可以方便的格式化Android程序:Java文件和XML文件,特别是Java文件,其配置是基于JDT实现的,所以对Java的方方面面的配置都覆盖了。

? ? ? ? 但是,只有这些还不够,对一般的Java设计编码原则和Android中推荐的风格还不能完全自动执行,还需要人的参与,依赖于团队风格规范的制定,成员的设计能力和领悟执行能力。下面是这些工具所不能解决的风格和原则:

?



文章来源《IT蓝豹》



推荐阅读:
  1. PHP 代码规范
  2. Python 代码规范

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

google 表达式 return

上一篇:MySQL高级学习笔记(三):Mysql逻辑架构介绍、mysql存储引擎详解

下一篇:Js中使用正则表达式验证输入是否有特殊字符

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》