小球在绳子上移动,利用面向对象的思想编写模型。
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,Point startPoint)
{
this.rope=rope;
this.currentPoint=startPoint;
}
public void move(){
currentPoint=rope.nextPoint(currentPoint);
System.out.println("小球移动到了"+currentPoint);
}
}