-------android培训、java培训、期待与您交流! ----------
面向对象的分析与设计
1.面向对象设计把握一个重要的经验:谁拥有数据,谁就对外提供操作这些数据的方法。
例子:
(1)人在黑板上画圆。
总的有三个对象:person,blackboard,circle
画圆需要圆心和半径。所以draw();方法是圆身上的。
人发出画圆的信号。黑板是载体。圆拥有画圆的方法。
(2)列车司机紧急刹车
列车司机发出信号刹车。列车自己提供刹车方法。因为只有列车知道怎么样刹车。
(3)售货员统计售货小票的金额
售货员只是得到了总金额。是小票自己提供的方法。
(4)你把门关上了。
人只是发出关门的信号。关门的动作是门自己提供的。
面向对象的面试题,用面向对象的方式设计如下情景:
1.两块石头磨成一把石刀,石刀可以砍树,砍成木材,木材做成椅子。
示例代码:
package ObjectOriented;
/*
* 石刀生成工厂
*/
public class KnifeFactory {
public StoneKnife createKnife(Stone first,Stone second){
return new StoneKnife();
}
}
package ObjectOriented;
/*
* 石刀类
*/
public class StoneKnife {
public Material cutTree(StoneKnife sk,Tree tree){
return new Material();
}
}
package ObjectOriented;
/*
* 椅子生成工厂
*/
public class ChairFactory {
public Chair makeChair(Material mt){
return new Chair();
}
}
2.球从一根绳子的一端移动到了另一端。
示例代码:
package ObjectOriented;
public class Ball {
private Rope rope ;
private Point startPoint;
public Ball(Rope rope, Point startPoint) {
super();
this.rope = rope;
this.startPoint = startPoint;
}
public void move(Point startPoint){
startPoint = rope.nextPoint(startPoint);
System.out.println("小球移动到了"+startPoint+"处");
}
}
class Point{
}
package ObjectOriented;
//绳子
public class Rope {
private Point startPoint;
private Point endPoint;
public Rope(Point startPoint, Point endPoint) {
super();
this.startPoint = startPoint;
this.endPoint = endPoint;
}
public Point nextPoint(Point currentPoint){
if(currentPoint==endPoint){
return null;
}
return currentPoint;
}
}