属性:轮子个数,轮子颜色
行为:跑(输出语句模拟:”瞪着跑”,输出语句中要有自行车的属性)
电动车类:
属性:轮子个数,轮子颜色,电池(布尔类型:真代表有电,假代表没电)
行为:跑(如果电池有电就骑着跑,如果电池没电只能瞪着跑)
注意:骑着跑用输出语句模拟,瞪着跑需要调用自行车类的跑方法
package kehouzuoye.zuoye04;
public class Bicycle {
public String getWheel() {
return wheel;
}
public void setWheel(String wheel) {
this.wheel = wheel;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
// 属性:轮子个数,轮子颜色
// 行为:跑(输出语句模拟:”瞪着跑”,输出语句中要有自行车的属性)
// 电动车类:
// 属性:轮子个数,轮子颜色,电池(布尔类型:真代表有电,假代表没电)
// 行为:跑(如果电池有电就骑着跑,如果电池没电只能瞪着跑)
// 注意:骑着跑用输出语句模拟,瞪着跑需要调用自行车类的跑方法
private String wheel;
private String color;
public void go(){
System.out.println("蹬着"+this.getWheel()+"轮子的"+this.getColor()+"的车");
}
}
上面是自行车类
package kehouzuoye.zuoye04;
public class ElectricVehicle extends Bicycle{
private boolean battery;
public void run() {
if(battery==true){
System.out.println("骑着"+this.getWheel()+"轮子的"+this.getColor()+"的车");
}else{
go();
}
}
public boolean isBattery() {
return battery;
}
public void setBattery(boolean battery) {
this.battery = battery;
}
}
上面是电动车类
下面是测试类
package kehouzuoye.zuoye04;
public class Test{
public static void main(String[] args) {
ElectricVehicle electricVehicle = new ElectricVehicle();
System.out.println("没电了");
electricVehicle.setColor("白色");
electricVehicle.setWheel("2个");
electricVehicle.setBattery(false);
electricVehicle.run();
Bicycle bicycle=(Bicycle) electricVehicle;
bicycle.setColor("黑色");
bicycle.setWheel("2个");
System.out.println("电充好了");
electricVehicle.setBattery(true);
electricVehicle.run();
}
}
标签:调用,Java,String,battery,void,electricVehicle,轮子,public,模拟
来源: https://blog.csdn.net/GuoWuZhu/article/details/100696185