老师,请问我的代码有哪些细节需要补充吗
来源:2-23 编程练习
Richard1001
2020-02-17 15:00:00
package com.imooc.garage;
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 bicycle=new Bicycle("捷安特","黄");
System.out.println(bicycle.work());
System.out.print("电动车类信息测试:");
ElectricVehicle electricvehicle=new ElectricVehicle("飞鸽");
System.out.println(electricvehicle.work());
System.out.print("三轮车类信息测试:");
Tricycle tricycle=new Tricycle();
System.out.println(tricycle.work());
}
}package com.imooc.garage;
public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String make;
private String color;
private int wheels=2;
private int seats=1;
public NonMotor() {
}
public NonMotor(String make, String color) {
this.setMake(make);
this.setColor(color);
}
public NonMotor(String make, String color, int wheels, int seats) {
this.setMake(make);
this.setColor(color);
this.setWheels(wheels);
this.setSeats(seats);
}
// 无参构造方法
// 双参构造方法,完成对品牌和颜色的赋值
// 四参构造方法,分别对所有属性赋值
// 公有的get***/set***方法完成属性封装
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWheels() {
return wheels;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
public int getSeats() {
return seats;
}
public void setSeats(int seats) {
this.seats = seats;
}
// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
public String work() {
String str="这是一辆"+this.getColor()+"颜色的,"+this.getMake()+"牌的非机动车,有"+this.getWheels()+"个轮子,有"+this.getSeats()+"个座椅";
return str;
}
}
package com.imooc.garage;
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle() {
super();
}
public Bicycle(String make, String color) {
super(make,color);
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str="这是一辆"+super.getColor()+"颜色的,"+super.getMake()+"牌的自行车";
return str;
}
}
package com.imooc.garage;
public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String battery;
public ElectricVehicle() {
super();
}
public ElectricVehicle(String battery) {
this.setBattery(battery);
}
// 公有的get***/set***方法完成属性封装
public String getBattery() {
return battery;
}
public void setBattery(String battery) {
this.battery = battery;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work() {
String str="这是一辆使用"+this.getBattery()+"牌电池的电动车";
return str;
}
}
package com.imooc.garage;
public class Tricycle extends NonMotor {
// 在无参构造中实现对轮子属性值进行修改
private int wheels=3;
public Tricycle() {
super.setWheels(wheels);
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str="三轮车是一款有"+super.getWheels()+"个轮子的非机动车";
return str;
}
}
1回答
同学你好,代码完成的很好,逻辑清晰~不需要修改了
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题