请老师帮忙看看,谢谢
来源:2-23 编程练习
ByteDancer07
2020-01-02 09:41:48
Test类
public class Test {
public static void main(String[] args) {
System.out.print("父类信息测试:");
NonMotor nonMotor = new NonMotor("天宇","红",4,2);
System.out.println(nonMotor.work());
System.out.print("自行车类信息测试:");
Bicycle bike = new Bicycle("捷安特","黄");
System.out.println(bike.work());
System.out.print("电动车类信息测试:");
ElectricVehicle vehicle = new ElectricVehicle();
System.out.print("三轮车类信息测试:");
Tricycle tricycle = new Tricycle();
System.out.println(tricycle.work());
}
}NonMotor类
public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String brand;
private String color;
private int wheel;
private int chair;
// 无参构造方法
public NonMotor(){
}
// 双参构造方法,完成对品牌和颜色的赋值
public NonMotor(String brand, String color) {
this.brand = brand;
this.color = color;
}
// 四参构造方法,分别对所有属性赋值
public NonMotor(String brand, String color, int wheel, int chair) {
this.setBrand(brand);
this.setColor(color);
this.setWheel(2);
this.setChair(1);
}
// 公有的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 getWheel() {
return wheel;
}
public void setWheel(int wheel) {
this.wheel = wheel;
}
public int getChair() {
return chair;
}
public void setChair(int chair) {
this.chair = chair;
}
// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
public String work() {
String str = ""+"这是一辆"+this.getColor()+"颜色的,"+this.getBrand()+"牌的非机动车,有"+
this.getWheel()+"个轮子,有"+this.getChair()+"个座椅的非机动车。";
return str;
}
}Bicyle类
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle(String bikeBrand,String bikeColor){
super(bikeBrand,bikeColor);
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str = ""+"这是一辆"+this.getColor()+"颜色的,"+this.getBrand()+"牌的自行车。";
return str;
}
}ElectricVehicle类
public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String elecBrand;
// 公有的get***/set***方法完成属性封装
public String getElecBrand() {
return elecBrand;
}
public void setElecBrand(String elecBrand) {
this.elecBrand = elecBrand;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work() {
String str = ""+"这是一辆使用"+this.getElecBrand()+"牌电池的电动车。";
return str;
}
}Tricycle类
public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String elecBrand;
// 公有的get***/set***方法完成属性封装
public String getElecBrand() {
return elecBrand;
}
public void setElecBrand(String elecBrand) {
this.elecBrand = elecBrand;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work() {
String str = ""+"这是一辆使用"+this.getElecBrand()+"牌电池的电动车。";
return str;
}
}1回答
同学你好,1、根据题目要求,非机动车轮子(默认2个)、座椅(默认 1个),并不是说非机动车必须轮子是两个、座椅是一个,同学可以设置这两个属性的默认值是2与1,例如:

在构造方法中,需要根据传入的参数赋值属性,而不是直接定义这两个属性的值

2、同学并没有贴出Tricycle类中的内容,建议同学将自己的代码完整贴出。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题