------- android培训、java培训、期待与您交流! ----------
面向对象设计把握一个重要的经验:谁拥有数据,谁就对外提供操作这些数据的方法。
牢牢掌握几个典型的案例:
1, 人在黑板上画圆
这里花园的方法到底是谁提供的呢?分析后我们可以知道画圆要使用圆心坐标和半径两个数据,而这两个数据是圆的数据(基本属性),所以,画圆这个方法是圆提供的,人只是调用了它。
2, 司机刹车
刹车这一动作是谁为我们提供的呢?当然是车了,这里的司机只是调用的车的刹车这一方法。
3, 售货员打印小票统计顾客消费金额
这里的统计顾客消费金额的这一方法是小票提供的,而售货员只是调用了票据对象的计算总额的方法计算出总额。
球从一根绳子的一段移动到了另一端
对象:Rope,Ball
Class Rope{
private Point start;
private Point end;
public Rope(Point start,Point end){
this.start=start;
this.end=end;
}
public Point nextPoint(Point currentPoint){
/* 通过两点一线的数学公式计算当前的下一个点,如果是终点,则返回null */
}
}
class Ball{
private Rope rope;
private Point currentPoint;
public Ball(Rope rope,startPoint){
this.rope=rope;
this.currentPoint=startPoint;
}
public void move(){
currentPoiont=rope.nextPoint(currentPoint);
System.out.println(“小球移动到+currentPoint);
}
}