1、面向对象设计的一个重要经验是,谁拥有数据,谁就提供操作这些数据的方法。
2、小球沿绳子移动,采用面向对象的分析
class Rope
{
private int startPoint;
private int endPoint;
private int currentPoint;
public int nextPoint()
{
return currentPoint + 1;
}
}
class Ball
{
private Rope rope;
private int currentPoint;
public Ball(Rope rope, int currentPoint) {
super();
this.rope = rope;
this.currentPoint = currentPoint;
}
public void Move()
{
currentPoint = rope.nextPoint();
System.out.println("小球移动到了"+currentPoint);
}
}
3、枚举灯的对象:
public enum Lamp {
S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),
S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);
private Lamp(String opposite,String next,boolean lighted){
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
private boolean lighted;
private String opposite;
private String next;
public boolean isLighted(){
return lighted;
}
public void light(){
this.lighted = true;
if(opposite != null){
Lamp.valueOf(opposite).light();
}
System.out.println(name() + " lamp is green);
}
public Lamp blackOut(){
this.lighted = false;
if(opposite != null){
Lamp.valueOf(opposite).blackOut();
}
Lamp nextLamp= null;
if(next != null){
nextLamp = Lamp.valueOf(next);
System.out.println(name() + next);
nextLamp.light();
}
return nextLamp;
}
}
-----------------------------------------------------------------------
以下为复习后笔记:
-----------------------------------------------------------------------
1、面向对象设计的一个重要经验:谁拥有数据,谁就提供对这些数据的操作方法。