看下是否可以改进
来源:2-23 编程练习
M灬spirit
2019-08-21 00:20:53
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("父类信息测试:");
NonMotor one=new NonMotor();
one.setBrand("天宇");
one.setColour("红");
one.setChair(1);
one.setWheel(2);
System.out.println(one.work());
System.out.print("自行车类信息测试:");
Bicycle two=new Bicycle("黄","捷安特",2,1);
System.out.println(two.work());
System.out.print("电动车类信息测试:");
ElectricVehicle three=new ElectricVehicle();
three.setCellbrand("飞鸽");
System.out.println(three.work());
System.out.print("三轮车类信息测试:");
Tricycle four=new Tricycle();
System.out.println(four.work());
}
}public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String brand;
private String colour;
private int wheel=2;
private int chair=1;
// 无参构造方法
public NonMotor() {
}
// 双参构造方法,完成对品牌和颜色的赋值
public NonMotor(String brand,String colour) {
this.setBrand(brand);
this.setColour(colour);
}
// 四参构造方法,分别对所有属性赋值
public NonMotor(String brand,String colour,int wheel,int chair) {
this.setBrand(brand);
this.setColour(colour);
this.setWheel(wheel);
this.setChair(chair);
}
// 公有的get***/set***方法完成属性封装
public void setBrand(String brand) {
this.brand=brand;
}
public String getBrand() {
return brand;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
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.getColour()+"颜色的,"+this.getBrand()+"牌的非机动车,"+"有"+this.getWheel()+"个轮子"+"有"+this.getChair()+"个座椅的非机动车。";
return str;
}
}
public class Bicycle extends NonMotor{
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle(String brand,String colour,int wheel,int chair) {
super(brand, colour, wheel, chair);
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str="这是一辆"+this.getColour()+"颜色的,"+this.getColour()+"牌的自行车。";
return str;
}
}
public class ElectricVehicle extends NonMotor{
// 私有属性:电池品牌
private String cellbrand;
// 公有的get***/set***方法完成属性封装
public String getCellbrand() {
return cellbrand;
}
public void setCellbrand(String cellbrand) {
this.cellbrand = cellbrand;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work() {
String str="这是一辆使用"+this.getCellbrand()+"牌电池的电动车。";
return str;
}
}
public class Tricycle extends NonMotor{
// 在无参构造中实现对轮子属性值进行修改
public Tricycle() {
this.setWheel(3);
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str="三轮车是一款有"+this.getWheel()+"个轮子的非机动车";
return str;
}
}
1回答
同学你好,代码完成的很棒!继续努力!不需要其他改进了~
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题