1. Immutable Objects and Classes

1.1 Immutable Objects and Immutable Classes

  • 如果一个object的内容在它创建之后不能再修改,则称这个object为immutable object,它所属的class也被称为immutable class
  • 例如,Circle类中删去了set method,整个类就会变为immutable因为radius是private的并且无法再通过set进行修改

1.2 Mutator and Accessor Methods

  • mutators(setter):修改object属性的方法
  • accessor(getter):读取object属性的方法
  • 需要注意的是,即使所有的data fields都是private并且没有setter,这个类也不一定是immutable的;如果对象内部包含其他可变对象的引用,getter也可能将该引用返回到外部,使外部间接修改内部object

Example

如果对象内部包含其他可变对象的引用,getter也可能将该引用返回到外部,使外部间接修改内部object的直观展示

  • Student Class:
public class Student{
	private int id;
	private BirthDate birthDate;
	
	public Student(int ssn,int year,int month,int day){
		id=ssn;
		birthDate=new BirthDate(year,month,day);    //对象内部包含其他可变对象的引用
	}
	
	public int getId(){    //accessor(getter)
		return id;	
	}
	
	public BirthDate getBirthDate(){    //accessor(getter)
		return birthDate;
	}
}
  • BirthDate Class:
public class BirthDate{    //本身是一个可变类
	private int year;
	private int month;
	private int day;
	
	public BirthDate(int newYear,int newMonth,int newDay){
		year=newYear;
		month=newMonth;
		day=newDay;
	}
	
	public void setYear(int newYear){    //mutator(setter)
		year=newYear;
	}
}
  • Test Class:
public class Test{
	public static void main(String[] args){
		Student student = new Student(112233,1970,1,1);
		BirthDate date = student.getBirthDate();
		date.setYear(2010);    //通过BirthDate完成了对Student的间接修改
	}
}

2. Scope of Variables

  • scope(作用域)指变量在程序中可以被访问和使用的范围

2.1 Instance and Static Variables

  • instance variable: 属于对象,每个对象拥有独立的数据
  • static variables:属于类,由所有对象共享
  • 二者的作用范围都是整个class,可以在class的任意位置声明

2.2 Local Variables

  • 在method, constructor或其他代码块的内部声明
  • 作用域从声明位置开始到所在代码块结束
  • 必须在使用前显式initialization

Example

public class Student{
	private int age;    //age:instance variable
	private static int count = 0;    //count:static variable
	
	public void setAge(int newAge){
		int temp = newAge;    //temp:local variable
		age = temp;
		count++;		
	}
}

3. The this Keyword

3.1 Referencing Hidden Data Fields

  • this代表指向当前对象自身的引用
  • 常用于访问被同名局部变量或参数隐藏的instance variable或在一个构造函数中调用同一个类的另一个构造函数

3.2 Calling Overloaded Constructors

  • constructor overloading:同一个类中存在多个名称相同但参数列表不同的构造函数
public class Circle{
	private double radius;
	
	public Circle(){    //无参构造方法
		radius = 1.0;
	}
	
	public Circle(double newRadius){    //有参构造方法
		radius = newRadius;
	}
}

4. Class Abstraction and Encapsulation

4.1 Class Abstraction and Class Contract

  • class abstraction:将类的实现(implementation)与使用(usage)分离,这样使用者只需要关注类的功能而无需了解实现方式
  • encapsulation(封装)将类的具体实现细节隐藏起来,同样使用者只需要知道如何使用类不需要了解内部如何实现
  • class contract则是类向外部提供的公开接口,包括method signatures和public constants等,规定使用者如何使用这个类

4.2 Designing the Loan Class

  • constructor:
public class Loan{
	private double annualInterestRate;
	private int numberOfYears;
	private double loanAmount;
	private java.util.Date loanDate;
	
	public Loan(){
		this(2.5, 1, 1000);    //调用有参构造方法
	}
	
	public Loan(double annualInterestRate,int numberOfYears,double loanAmount){
		this.annualInterestRate = annualInterestRate;
		this.numberOfYears = numberOfYears;
		this.loanAmount = loanAmount;
		loanDate = new java.util.Date();	
	}
}
  • methods:
//AnnualInterestRate/NumberOfYears/LoanAmount的getter和setter(略)
//此处是encapsulation的体现(保护内部数据并提供访问接口)
 
 
	public double getMonthlyPayment(){
		double monthlyInterestRate = annualInterestRate/1200;
		double monthlyPayment = loanAmount*monthlyInterestRate/(1-(Math.pow(1/(1+monthlyInterestRate),numberOfYears*12)));
		return monthlyPayment;
	}
 
	public double getTotalPayment(){
		double totalPayment = getMonthlyPayment()*numberOfYears*12;
		return totalPayment;
	}
	
	public java.util.Date getLoanDate(){
		return loanDate;
	}
//以上method是abstraction的体现(提供功能隐藏实现过程)

5. Object-Oriented Thinking

  • 面向对象编程带来了更好的:
    • flexibility: 更容易修改和扩展
    • modularity: 将程序拆分成独立的类
    • reusability: 同一个类可以在不同程序中重复使用
  • procedural programming:以执行步骤和方法为中心,通常分开组织数据与方法
  • object-oriented programming:以对象和类为中心,将数据与方法组织在同一个类中

6. Class Design Guidelines

6.1 Coherence

  • 一个class只应该描述一个明确的实体,其属性和方法都应该围绕同一个目标

6.2 Separating Responsibilities

  • 如果一个类承担过多职责,就应当将其拆分为多个Class

6.3 Name Conventions and Consistency

  • 遵循Java标准命名规范
  • 命名应该 meaningful and descriptive
  • variable和method名通常第一个单词小写,之后每个单词首字母大写(如computeArea)
  • class名则每个单词首字母都大写(如ComputeArea)
  • constant名则全部大写(如MAX_VALUE)

6.4 Using Visibility Modifiers

Modifier主要作用
private隐藏内部数据和方法
protected向子类等允许的代码提供访问权限
public向外部提供公开接口

6.5 Using the static Modifier

  • static定义所有对象共享的属性, 例如private static int count = 0;

6.6 Clarity

  • 类的使用方式应直观、灵活,避免不必要的调用顺序依赖

链接