知识点
相关文章
更多最近更新
更多《JAVA代码规范》(十一)编程惯例、其它惯例、代码范例 (7-10)
2019-03-28 17:37|来源: 苦力工
7 编程惯例
7.1 提供对实例以及类变量的访问控制
若没有足够理由,不要把实例或类变量声明为公有。一个具有公有实例变量的恰当例子,是类仅作为数据结构,没有行为。亦即,若你要使用一个结构(struct)而非一个类(如果java支持结构的话),那么把类的实例变量声明为公有是合适的。
7.2 引用类变量和类方法
避免用一个对象访问一个类的静态变量和方法。应该用类名替代。例如:
classMethod(); //OK
AClass.classMethod(); //OK
anObject.classMethod(); //AVOID!
7.3 常量
位于for循环中作为计数器值的数字常量,除了-1,0和1之外,不应被直接写入代码。
7.4 变量赋值
避免在一个语句中给多个变量赋相同的值。它很难读懂。例如:
fooBar.fChar = barFoo.lchar = 'c'; // AVOID!
不要将赋值运算符用在容易与相等关系运算符混淆的地方。例如:
if (c++ = d++) { // AVOID! (Java disallows)
...
}
应该写成
if ((c++ = d++) != 0) {
...
}
不要使用内嵌(embedded)赋值运算符试图提高运行时的效率,这是编译器的工作。例如:
d = (a = b + c) + r; // AVOID!
应该写成
a = b + c;
d = a + r;
8 其它惯例
8.1 圆括号
一般而言,在含有多种运算符的表达式中使用圆括号来避免运算符优先级问题,是个好方法。即使运算符的优先级对你而言可能很清楚,但对其他人未必如此。你不能假设别的程序员和你一样清楚运算符的优先级。
if (a == b && c == d) // AVOID!
if ((a == b) && (c == d)) // RIGHT
8.2 返回值
设法让你的程序结构符合目的。例如:
if (booleanExpression) {
return true;
} else {
return false;
}
应该代之以如下方法:
return booleanExpression;
类似地:
if (condition) {
return x;
}
return y;
应该写做:
return (condition ? x : y);
8.3 条件运算符"?"前的表达式
如果一个包含二元运算符的表达式出现在三元运算符" ? : "的"?"之前,那么应该给表达式添上一对圆括号。例如:
(x >= 0) ? x : -x;
8.4 特殊注释
在注释中使用XXX来标识某些未实现(bogus)的但可以工作(works)的内容。用FIXME来标识某些假的和错误的内容。
9 代码范例
9.1 源文件范例
下面的例子,展示了如何合理布局一个包含单一公共类的Java源程序。接口的布局与其相似。更多信息参见"类和接口声明"以及"文挡注释"。
/*
* @(#)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...
}
}
10 缩写说明
SEPG
Software Engineering Process Group(软件工程过程化组织)缩写。
本文链接:领悟书生教程网,转自ajava.org
相关问答
更多-
求JAVA多线程编程代码[2023-12-20]
测试过了,没问题。基本思路,实例化一个桥类,谁得到桥的可用标志谁过桥。 我第一个看到这个100分的,说实话,知道你是个学生要代码而已,线程类好久没练手了,练习一下而已,否则真不会给你写代码。因为我读书的时候也发过类似的求助,知道什么感受。不懂的时候真的没办法,所以告诉你思路。 package cn.thread; public class Through_out_bridge { public static void main(String[] args) { Bridge b = Bridge.getIn ... -
IE9在开始从Amazon S3流式传输视频之前等待7-10秒(IE9 Waiting 7-10 seconds before starting to stream a video from Amazon S3)[2023-07-15]
MP4在文件末尾有索引,因此Internet Explorer在开始播放之前下载整个文件。 我不知道为什么会这样做,但你可以在MP4上运行QTIndexSwapper来解决问题。 http://renaun.com/blog/code/qtindexswapper/ That MP4 has the index at the end of the file so Internet Explorer downloads the entire file before starting playback. I d ... -
Caliburn Micro约定功能使用x:Name指令而不是Name属性。