老师看下可以吗?谢谢
来源:2-23 编程练习
SELECT_NULL
2020-01-13 17:23:47
父类:
package com.imooc.animal;
/**
* 属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
*
* 方法:
*
* 编写无参构造方法、双参构造方法和四参构造方法,其中,在双参构造方法中,完成对品牌和颜色的赋值;在四参构造方法中,完成对所有属性的赋值
*
* 编写运行的方法,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
*
* @author dou
*
*/
public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String brand;
private String color;
private int wheelNum = 2;
private int seatNum = 1;
// 无参构造方法
public NonMotor() {
}
// 双参构造方法,完成对品牌和颜色的赋值
public NonMotor(String brand, String color) {
this.setBrand(brand);
this.setColor(color);
}
// 四参构造方法,分别对所有属性赋值
public NonMotor(String brand, String color, int wheelNum, int seatNum) {
this.setBrand(brand);
this.setColor(color);
this.setWheelNum(wheelNum);
this.setSeatNum(seatNum);
}
// 公有的get***/set***方法完成属性封装
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWheelNum() {
return wheelNum;
}
public void setWheelNum(int wheelNum) {
this.wheelNum = wheelNum;
}
public int getSeatNum() {
return seatNum;
}
public void setSeatNum(int seatNum) {
this.seatNum = seatNum;
}
// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
public String work() {
String str = "这是一辆" + this.getColor() + "颜色的," + this.getBrand() + "牌的非机动车,有" + this.getWheelNum() + "个轮子,有"
+ this.getSeatNum() + "个座椅的非机动车。";
return str;
}
}自行车类:
package com.imooc.animal;
/**
* 自行车类:
在构造方法中调用父类多参构造,完成属性赋值
重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
* @author 123
*
*/
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle() {
super("捷安特", "黄");
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str = "这是一辆" + this.getColor() + "颜色的," + this.getBrand() + "牌的自行车。";
return str;
}
}电动车类:
package com.imooc.animal;
/**
* 电动车:
增加“电池品牌”属性
重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
* @author dou
*
*/
public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String batteryBrands;
// 公有的get***/set***方法完成属性封装
public ElectricVehicle(String batteryBrands) {
this.setBatteryBrands(batteryBrands);
}
public String getBatteryBrands() {
return batteryBrands;
}
public void setBatteryBrands(String batteryBrands) {
this.batteryBrands = batteryBrands;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work() {
String str = "这是一辆" + this.getBatteryBrands() + "牌电池的电动车。";
return str;
}
}三轮车类:
package com.imooc.animal;
/**
* 三轮车:
在无参构造中实现对轮子属性值进行修改
重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
* @author DOU
*
*/
public class Tricycle extends NonMotor {
// 在无参构造中实现对轮子属性值进行修改
public Tricycle() {
super.setWheelNum(3);
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str = "三轮车是一款有"+this.getWheelNum()+"个轮子的非机动车";
return str;
}
}测试类:
package com.imooc.test;
import com.imooc.animal.Bicycle;
import com.imooc.animal.ElectricVehicle;
import com.imooc.animal.NonMotor;
import com.imooc.animal.Tricycle;
public class NonMotorTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
NonMotor one=new NonMotor("天宇","红",4,2);
Bicycle two=new Bicycle();
ElectricVehicle three =new ElectricVehicle("飞鸽");
Tricycle four=new Tricycle();
System.out.print("父类信息测试:");
System.out.println(one.work());
System.out.print("自行车类信息测试:");
System.out.println(two.work());
System.out.print("电动车类信息测试:");
System.out.println(three.work());
System.out.print("三轮车类信息测试:");
System.out.println(four.work());
}
}1回答
同学你好,代码有如下问题:
测试Bicycle的时候答案虽然是正确的,但却把自行车品牌和颜色固定为了“捷安特”“黄”,Bicycle中的构造方法修改建议如下:

在测试方法中 ,调用多参构造可实现自行车品牌和颜色的动态绑定

如果我的回答解决你的疑惑,请采纳,祝学习愉快~~~
相似问题