您的位置:首页 > 职场人生

Java面试题

2013-07-07 15:43 134 查看
交通灯系统
面向对象设计把我一个重要的经验:谁拥有数据,谁就对外提供操作这些数据的方法。

典型案例:

球从绳子的一端移动到了另一端:

class Rope {

privatePoint start;

privatePoint end;

publicRope(Point start,Point end){

this.start=start;

this.end=end;

}

publicPoint nextPoint(Point currentPoint){

/* 通过两点一线的数学公式可以计算出当前点的下一个点,

如果当前点是终止点,则返回null,如果当前点不是线上的点,则抛出异常*/

return null;

}

}

class Ball{

private Rope rope;

private Point currentPoint;

public Ball(Rope rope,PointstartPoint){

this.currentPoint=startPoint;

this.rope=rope;

}

public void move(){

currentPoint=rope.nextPoint(currentPoint);

System.out.println("小球移动到了"+currentPoint);

}

}


两块石头磨成一把石刀,石刀可以砍树,砍成木材,木材做成椅子

StoneKnife knifeFactory(Stone first,Stone second);

Material stoneKnife.cut(Tree tree);

Chair chairFactory(Material material);

JDK1.5以后创建线程的新方法:线程池

//模拟车辆不断随机上路的过程

ExecutorServicepool = Executors.newSingleThreadExecutor();

pool.execute(new Runnable(){

publicvoid run(){

for(int i=1;i<1000;i++){

try {

Thread.sleep((new Random().nextInt(10) +1) * 1000);

}catch(InterruptedException e) {

e.printStackTrace();

}

vechicles.add(Road.this.name + "_" + i);

}

}

});


定时器:

ScheduledExecutorService timer =  Executors.newScheduledThreadPool(1);

timer.scheduleAtFixedRate(

new Runnable(){

publicvoid run(){

if(vechicles.size()>0){

boolean lighted = Lamp.valueOf(Road.this.name).isLighted();

if(lighted){

System.out.println(vechicles.remove(0) + " is traversing !");

}

}

}

},

1,

1,

TimeUnit.SECONDS);


银行业务调度系统:

有三种对应类型的客户:VIP客户,普通客户,快速客户,异步随机生成各种类型的客户,各类型客户在其对应窗口按顺序依次办理业务

各类型客户在其对应窗口按顺序依次办理业务,准确地说,应该是窗口依次叫号。




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: