BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #36645同步于 2014/11/26
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

面向对象面试题,青蛙爬井的故事

studychina
2014/11/26镜像同步7 回复
青蛙爬井,白天爬上3米;晚上掉下1米。请用面向对象的思维构建出相关的类,对象;以及他们之间的关系。 大家说说怎么构建呀,可以随便发表。好像也没有标准答案的说法。
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
taoch机器人#1 · 2014/11/26
class Frog{ private int height; private static final int up; private static final int down; //构造函数略,传入上升下降具体量值 public int getHeight(){ return this.height; } public void up(){ this.height += up; } public void down(){ height -= down; if(height < 0){ height = 0; } } } class Wall{ private int wallHeight; //构造函数略,传入墙高值 public int getWallHeight(){ return wallHeight; } } class World{ public void danTengDeQingWaQuestion(){ Wall wall = new Wall(xx); Frog frog = new Frog(xx,xx); while(true){ frog.up(); if(frog.getHeight() > wall.getWallHeight()){ syso("终于出来了"); break; } frog.down(); } } public static void main(String args[]){ World w = new World(); w.xxxxxxx(); } } 【 在 studychina (Michael) 的大作中提到: 】 : 青蛙爬井,白天爬上3米;晚上掉下1米。请用面向对象的思维构建出相关的类,对象;以及他们之间的关系。 : 大家说说怎么构建呀,可以随便发表。好像也没有标准答案的说法。
studychina机器人#2 · 2014/11/26
写的挺好的啊,估计花不少时间了。对了,青蛙和井类在面向对象中属于什么关系?是关联关系吗? 多谢~ 【 在 taoch 的大作中提到: 】 : class Frog{ : private int height; : private static final int up; : ...................
taoch机器人#3 · 2014/11/26
我那种写法算是并列的吧,相互本身没什么关系,只有在这个具体问题中才建立了关系。当然也可以写成一个是另一个的组成的形式,对于这个case都是可以的 【 在 studychina (Michael) 的大作中提到: 】 : 写的挺好的啊,估计花不少时间了。对了,青蛙和井类在面向对象中属于什么关系?是关联关系吗? : 多谢~
nuanyangyang机器人#4 · 2014/11/26
不懂。这里面没有明显的对象啊。 int height=0; while(true) { height += 3; height -= 1; }
studychina机器人#5 · 2014/11/27
关联(Association): 意义:" ... has a ..." 依赖(Dependency): 意义:" ... uses a ..."
nuanyangyang机器人#6 · 2014/11/27
用brainfuck写试试看 ------------------ 哎哟,掉下去了 ----------- 继续掉,继续掉 ------------- ------ 到底了 [+++-] 爬呀爬,爬呀爬
realmofwind机器人#7 · 2014/11/27
//井 class Well{ //井高度 private int height; public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } } interface IFrog{ public final static Context day = new Day(); public final static Context night = new Night(); public void jump(); } //青蛙 class Frog implements IFrog{ //青蛙一共跳了多少米 private int jumpHeight = 0; //青蛙外界环境:白天或夜晚 private Context context; public Context getContext() { return context; } public void setContext(Context context) { this.context = context; } public int getJumpHeight() { return this.jumpHeight; } @Override //青蛙跳 public void jump() { //白天跳 if(this.context.getClass().isInstance(day)){ this.jumpHeight += 2; System.out.println("白天跳上去2米"); //天黑了 this.context = this.context.turn(); } else if(this.context.getClass().isInstance(night)){ //晚上跳 this.jumpHeight -= 1; System.out.println("晚上掉了1米"); //天亮了 this.context = this.context.turn(); } } } //环境接口 interface Context{ //黑白交替 public Context turn(); } //白天 class Day implements Context{ @Override //天黑了 public Night turn() { return new Night(); } } //黑夜 class Night implements Context{ @Override //天亮了 public Day turn() { return new Day(); } } public class Client { public static void main(String[] args){ //得到一口井 Well well = new Well(); //设置井的高度 well.setHeight(100); //一只青蛙 Frog frog = new Frog(); //青蛙准备在白天开始跳 frog.setContext(IFrog.day); while(frog.getJumpHeight() < well.getHeight()){ //青蛙跳 frog.jump(); } System.out.println("跳出来了~一共跳了" + frog.getJumpHeight() +"米"); } }