Java Java code conventions Class Name Version information


























![[4]强制转型后应该跟一个空格,例如: my. Method((byte) a. Num, (Object) x); my. Method((int) (cp + 5), ((int) (i [4]强制转型后应该跟一个空格,例如: my. Method((byte) a. Num, (Object) x); my. Method((int) (cp + 5), ((int) (i](https://slidetodoc.com/presentation_image_h2/dc947176da3d5754bc191468d6da88b0/image-27.jpg)


![[5] 实例变量 : 大小写规则和变量名相似,除了前面 需要一个下划线,例如: Integer _employee. Id; String _name; Customer _customer; [6]常量 : [5] 实例变量 : 大小写规则和变量名相似,除了前面 需要一个下划线,例如: Integer _employee. Id; String _name; Customer _customer; [6]常量 :](https://slidetodoc.com/presentation_image_h2/dc947176da3d5754bc191468d6da88b0/image-30.jpg)
















- Slides: 46
Java语言编码规范 Java code conventions
上面的格式可以归纳为: /* * Class. Name * * Version information * * Date * * Copyright notice */ 4
过长的方法调用, 利用逗号换行 some. Method(long. Expression 1, long. Expression 2, long. Expression 3, long. Expression 4, long. Expression 5); 以下是两个断开算术表达式的例子。前者更好,因为断开处位于 括号表达式的外边,这是个较高级别的 断开 long. Name 1 = long. Name 2 * (long. Name 3 + long. Name 4 - long. Name 5) + 4 * longname 6; //好的断开 long. Name 1 = long. Name 2 * (long. Name 3 + long. Name 4 - long. Name 5) + 4 * longname 6; //不好的断开 9
以下是两个缩进方法声明的例子。前者是常规情形。后者若使用 常规的缩进方式将会使第二行和第三行 移得很靠右,所以代之以缩进 8个空格 some. Method(int an. Arg, Object another. Arg, String yet. Another. Arg, Object and. Still. Another) {. . . } private static synchronized horking. Long. Method. Name(int an. Arg, Object another. Arg, String yet. Another. Arg, Object and. Still. Another) {. . . } 10
三种可行的方法用于处理三元运算表达式: alpha = (a. Long. Boolean. Expression) ? beta : gamma; //如果上面的太长 alpha = (a. Long. Boolean. Expression) ? beta : gamma; //如果上面的还是太长 alpha = (a. Long. Boolean. Expression) ? beta : gamma; 11
声明 推荐一行一个声明,因为这样以利于写注释。亦即, int level; // indentation level int size; // size of table 反对下面的做法: int level, size; //糟糕 注意, 在上面推荐的做法中, 类型和标识符之间放了一个空 格,另一种被允许的替代方式是使用制表符: int level; // indentation level int size; // size of table Object current. Entry; // currently selected table entry 17
void my. Method() { int 1 = 0; // beginning of method block if (condition) { int 2 = 0; . . . } // beginning of "if" block } 该规则的一个例外是for循环的索引变量: for (int i = 0; i < max. Loops; i++) {. . . } 19
If语句 三种规范格式: if (condition) { statements; } ======================== if (condition) { statements; } else{ statements; } 需要注意的是: 1) 不要因为statements只有一句就跟contion写到同一行: 例如: if (condition){ statements; } 22
2) 也不要因为 statements 只有一句, 就省略 大括号, 例如: if (condition) statement; 总之, 无论如何, 一定要写成 if (condition){ statements; } 23
[4]强制转型后应该跟一个空格,例如: my. Method((byte) a. Num, (Object) x); my. Method((int) (cp + 5), ((int) (i + 3)) + 1); 26
[5] 实例变量 : 大小写规则和变量名相似,除了前面 需要一个下划线,例如: Integer _employee. Id; String _name; Customer _customer; [6]常量 : 应该全部大写,单词间用下划线隔开。例 如: static final int MIN_WIDTH = 4; static final int MAX_WIDTH = 999; static final int GET_THE_CPU = 3; 29
3. 修改对于体积大小的限制:目前,很多显示器都是 17寸,而且打印方面的限制也比以前 有所改善,同时,由于代码中Factory等模式的运用,以及有意义的方法名称等约定,默 认每行代码的长度(80)是远远不能满足要求;对于方法长度等等,也应该根据项目情 况自行决定: <module name=“File. Length”/> <module name=“Line. Length”> <property name=“max” value=“ 120”/> </module> <module name=“Method. Length”> <property name=“max” value=“ 300”/> </module> <module name=“Parameter. Number”/> 4. 修改对于Throws的的限制:允许Throws Unchecked Exception以及Throws Subclass Of Another Declared Exception。 <module name="Redundant. Throws"> <property name="allow. Unchecked" value="true"/> <property name="allow. Subclasses" value="true"/> </module> 35
5. 修改成员变量的可视性:一般情况下,应该允 许Protected Members以及Package Visible Members。 <module name="Visibility. Modifier"> <property name="protected. Allowed" value="true"/> <property name="package. Allowed" value="true"/> </module> 36
39
43
Thanks! 45