老师,请帮我看一下是否需要改进
来源:2-22 编程练习
荒野56
2020-07-28 20:14:37
public class Test {
public static void main(String[] args) {
NonMotor one=new NonMotor("天宇","红",4,2);
System.out.println("父类信息测试:"+one.work());
Bicycle two =new Bicycle("捷安特","黄");
System.out.println("自行车类信息测试:"+two.work());
ElectricVehicle three=new ElectricVehicle("飞鸽");
System.out.println("电动车类信息测试:"+three.work());
Tricycle four=new Tricycle();
System.out.println("三轮车类信息测试:"+four.work());
}
}
public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String name;
private String colour;
private int whell;
private int chair;
// 无参构造方法
public NonMotor() {
}
// 双参构造方法,完成对品牌和颜色的赋值
public NonMotor(String name, String colour) {
this.setName(name);
this.setColour(colour);
}
// 四参构造方法,分别对所有属性赋值
public NonMotor(String name, String colour, int whell, int chair) {
this.setName(name);
this.setColour(colour);
this.setWhell(whell);
this.setChair(chair);
}
// 公有的get***/set***方法完成属性封装
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public int getWhell() {
if (this.whell == 0)
this.whell = 2;
return whell;
}
public void setWhell(int whell) {
this.whell = whell;
}
public int getChair() {
if (this.chair == 0)
this.chair = 1;
return chair;
}
public void setChair(int chair) {
this.chair = chair;
}
// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
public String work() {
String str="这是一辆"+this.getColour()+"颜色的,"+this.getName()+"牌的非机动车,有"+this.getWhell()+"个轮子,有"+this.getChair()+"个座椅的非机动车。";
return str;
}
}
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle(String name,String colour) {
super(name,colour);
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str="这是一辆"+this.getColour()+"颜色的,"+this.getName()+"牌的自行车。";
return str;
}
}
public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String battery;
// 公有的get***/set***方法完成属性封装
public ElectricVehicle(String battery) {
super();
this.setBattery(battery);
}
public String getBattery() {
return battery;
}
public void setBattery(String battery) {
this.battery = battery;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work() {
String str="这是一辆使用"+this.getBattery()+"牌电池的电动车。";
return str;
}
}
public class Tricycle extends NonMotor {
// 在无参构造中实现对轮子属性值进行修改
public Tricycle() {
super.setWhell(3);
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str="三轮车是一款有"+this.getWhell()+"个轮子的非机动车。";
return str;
}
}
1回答
嗯嗯,没问题。祝:学习愉快
相似问题
回答 1
回答 1