在网站上做承诺,昆山小程序制作,供水开发建设公司网站,安防网站源码1.乌龟和兔子共有属性和方法 做成父类 避免重复代码package com.lovo;/*** 类#xff1a; 动物* author Abe* 属性#xff1a; 名字 步距 总距离 睡觉的日子*/public class Animal {protected String name;protected int step;protected int distance;protected int sleepDay…1.乌龟和兔子共有属性和方法 做成父类 避免重复代码package com.lovo;/*** 类 动物* author Abe* 属性 名字 步距 总距离 睡觉的日子*/public class Animal {protected String name;protected int step;protected int distance;protected int sleepDay;/*** 构造器* param name*/public Animal(String name) {this.name name;}public Animal(){}/*** 方法获取 名字 行走长度 休息的天数 设置总距离 设置休息天数* return*/public String getName() {return name;}public int getStep() {return step;}public int getDistance(){return distance;}public int getSleepDay() {return sleepDay;}public void setDistance(int dis) {this.distance dis;}public void setSleepDay(int slp) {this.sleepDay slp;}}2.类乌龟package com.lovo;/*** 类 乌龟* author Abe* 父类Animal* 属性疲劳的日期*/public class Tortoise extends Animal {private int encourageDay;/*** 构造器* param name*/public Tortoise(String name) {super(name);}/*** 方法 走路每次2~4米*/public void walk() {if (encourageDay 0 sleepDay 0) {step (int) (Math.random() * 3 2);}}/*** 方法30%鼓励 之后3天每天可以走双倍的路程但是之后的第4天只能休息。*/public void encourage() {if (encourageDay 0 sleepDay 0 Math.random() 0.7) {step (int) (Math.random() * 3 2) * 2;encourageDay 3;sleepDay 1;}}/*** 方法 调用、设置 激励天数** return*/public int getEncourageDay() {return encourageDay;}public void setEncourageDay(int enc) {this.encourageDay enc;}/*** 方法每天的行动 1.没激励不睡觉 就看是否发动技能 没发动就普通移动 2.有激励就激励移动 没激励有睡觉就睡觉* return 每天活动的结果*/public String everyDay() {if (this.getEncourageDay() 0 this.getSleepDay() 0) {this.encourage();if (this.getEncourageDay() 0) {this.setDistance(this.getStep());return this.getName() 发动了技能《底力》疯狂的向前跑出了 this.getStep() 米;} else {this.walk();this.setDistance(this.getStep());return this.getName() 跑了 this.getStep() 米;}} else if (this.getEncourageDay() ! 0) {this.setDistance(this.getStep());this.setEncourageDay(-1);return this.getName() 的《底力》持续中它疯狂的向前跑出了 this.getStep() 米;} else {this.setDistance(0);this.setSleepDay(-1);return this.getName() 美美的睡了一觉~;}}}3.类兔子package com.lovo;/*** 类兔子* author Abe* 父类Animal*/public class Rabbit extends Animal {/*** 构造器*/public Rabbit(String name) {super(name);}/*** 方法跑步 每次可以跑1~16 但是一次跑步超过6就要休息1天超过11就要休息2天*/public void run() {step (int) (Math.random() * 16 1);if (step 11) {sleepDay 2;} else if (step 6) {sleepDay 1;}}/*** 方法没睡觉就跑步*/public String everyDay(){if(this.getSleepDay() 0 ){this.run();this.setDistance(this.getStep());if (this.getStep() 11) {return this.getName() 不可思议的跑出了 this.getStep() 米!!!~ 它累的不得不休息两天……;}else if(this.getStep() 6){return this.getName() 惊人的跑出了 this.getStep() 米!!~ 它累的不得不休息一天……;}else{return this.getName() 跑了 this.getStep() 米!;}}else{this.setDistance(0);this.setSleepDay(-1);return this.getName() 懒洋洋的睡了一觉~~;}}}4.开始赛跑吧~~)package com.lovo;/*** 开始比赛跑步 先跑完100米获胜* author Abe*/public class Running {public static void main(String[] args) {Tortoise tor new Tortoise(小乌龟);Rabbit rab new Rabbit(小兔子);for(int distance 100 , day 1 ;tor.getDistance() 100 rab.getDistance() 100 ;day ){System.out.println(-----------第 day 天-----------);System.out.println(tor.everyDay());System.out.println(rab.everyDay());if(tor.getDistance() 100 rab.getDistance() 100){System.out.println(怎么回事 tor.getName() rab.getName() 同时冲过了终点 居然是平局);}else if(tor.getDistance() 100){System.out.println(tor.getName() 取得了胜利);}else if(rab.getDistance() 100){System.out.println(rab.getName() 取得了胜利);}else{System.out.println(tor.getName() 一共跑了 tor.getDistance() 米!);System.out.println(rab.getName() 一共跑了 rab.getDistance() 米!);}}}}