回复 7楼 fengsjack
程序代码:
一:类的定义风格
代码1:不良的写法
class Employee
{
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January
hireDay = calendar.getTime();
}
private String name;
public String getName()
{
return name;
}
private double salary;
public double getSalary()
{
return salary;
}
private Date hireDay;
public Date getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
}
代码2:良好的写法
class Employee
{
private String name;
private double salary;
private Date hireDay;
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January
hireDay = calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
}
评:代码1 成员变量布局混乱,想到哪写到哪,很难整体把握类的功能。
二:实例域的类型
代码1:不良的写法
class Employee
{
public String name;
public double salary;
public Date hireDay;
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January
hireDay = calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
}
代码2:良好的写法
class Employee
{
private String name;
private double salary;
private Date hireDay;
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January
hireDay = calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
}
评:代码1 用public标记实例域,是一种极不提倡的做法。public 数据域允许程序的任何方法
对其进行读取和修改。这就完全破坏了封装。
三:
代码1:不好的写法
class Employee
{
private String name;
private double salary;
private Date hireDay;
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January
hireDay = calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
代码2:良好的写法
class Employee
{
private String name;
private double salary;
private Date hireDay;
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January
hireDay = calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise = this.salary * byPercent / 100;
this.salary += raise;
}
评:代码2 用隐式参数 this 将实例域与局部变量明显的区分开来了,便于代码的阅读。
四:静态方法与静态常量
代码1:不良的写法
class Employee
{
......
private static int nextId = 1;
public static int getNextId()
{
return nextId;
}
}
......
Employee harry = new Employee();
int n = harry.getNextId();
代码2:良好的写法
class Employee
{
......
private static int nextId = 1;
public static int getNextId()
{
return nextId;
}
}
......
int n = Employee.getNextId();
评:静态类成员不属于任何独立的类,它属于类。代码2 用harry.getNextId() 代替
Employee.getNextId(), 这种方式很容易造成混淆,getNextId()方法计算的结果
与 harry 毫无关系
五:默认域的初始化
代码1:不良的写法
class Employee
{
private int id;
private String name;
private double salary;
// the default constructor
public Employee()
{
}
......
}
代码2:良好的写法
class Employee
{
private int id;
private String name;
private double salary;
// the default constructor
public Employee()
{
name = "";
id = 0;
salary = 0;
}
......
}
评:代码1 在构造器中没有显示的给域赋予明确的初值, 会影响代码的可读性。
六:参数名
代码1:不良的写法
public Employee(String n, double s)
{
name = n;
salary = s;
}
代码2:良好的写法
public Employee(String aName, double aSalary)
{
name = aSalary;
salary = aName;
}
评:代码1 参数名过短,只能通过阅读代码才能了解参数n和参数s的含义。代码2 给
每个参数前面加了一个前缀"a", 这样就代码就很清晰了。每一个读者一眼就能够
看懂参数的含义。
七:阻止继承
代码1:不好的写法
class Employee
{
......
private String name;
private double salary;
private Date hireDay;
public String getName()
{
return name;
}
}
代码2:良好的写法
class Employee
{
......
private String name;
private double salary;
private Date hireDay;
public final String getName()
{
return name;
}
}
评:代码2 将类声明为 final, 可以确保它们不会在子类中改变语义。防止多态的滥用。
八:将公共操作和域放在超类
代码1:不良的写法
abstract class Person
{
......
}
class Employee extends Person
{
private String name;
......
}
代码2:良好的写法
abstract class Person
{
private String name;
}
class Employee extends Person
{
......
}
评:应该尽可能的将公共操作和域放在超类中,这样就可以最大限度的节省代码量。
九:滥用继承
代码1:不良的写法
class Contractor extends Employee
{
private double hourlyWage;
}
代码2:良好的写法
class Employee
{
......
}
class Contractor
{
private double hourlyWage;
}
评:除非所有继承的方法都有意义,否则不要使用继承
十:强制类型转换
代码1:不良的写法
class Employee
{
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
hireDay = calendar.getTime();
}
......
}
class Manager extends Employee
{
......
public void setBonus(double b)
{
bonus = b;
}
private double bonus;
}
Employee p = (Manager)manager;
p.setBonus();
......
代码2:良好的写法
class Employee
{
public void setBonus(double b)
{
bonus = b;
}
......
}
class Manager extends Employee
{
......
private double bonus;
}
Employee p = new Employee();
p.setBonus();
评:如果超类对象需要调用子类对象的方法,那么就应该检查一下超类的设计。重新设计
一下超类,添加相应的方法才是正确的选择
[
本帖最后由 BlueGuy 于 2010-8-4 22:05 编辑 ]