Java继承(上)练习2-22,烦请老师检查和指正~

来源:2-21 编程练习

Heijyu

2021-09-18 10:34:07

父类
​public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String brand;
private String color;
private int wheel =2;
private int seat = 1;
// 无参构造方法
public NonMotor(){

}
// 双参构造方法,完成对品牌和颜色的赋值
public NonMotor(String brand, String color){
this.setBrand(brand);
this.setColor(color);
}
// 四参构造方法,分别对所有属性赋值
public NonMotor(String brand, String color, int wheel, int seat){
this.setBrand(brand);
this.setColor(color);
this.setWheel(wheel);
this.setSeat(seat);
}
// 公有的get***/set***方法完成属性封装
public String getBrand(){
return this.brand;
}

public void setBrand(String brand){
this.brand = brand;
}

public String getColor(){
return this.color;
}

public void setColor(String color){
this.color = color;
}

public int getWheel(){
return this.wheel;
}

public void setWheel(int wheel){
this.wheel = wheel;
}

public int getSeat(){
return seat;
}

public void setSeat(int seat){
this.seat = seat;
}

// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
public String work() {
String str = "这是一辆" + this.getColor() + "颜色的," + this.getBrand() + "牌的非机动车,有" + this.getWheel() + "有" + this.getSeat() + "个座椅的非机动车。";
return str;
}
}

子类1
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle(String brand, String color){
super(brand, color);
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str = "这是一辆" + this.getColor() + "颜色的," + this.getBrand() + "牌的自行车。";
return str;
}

}

子类2
public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String battery;

public ElectricVehicle(String battery){
this.setBattery(battery);
}
// 公有的get***/set***方法完成属性封装
public String getBattery(){
return this.battery;
}

public void setBattery(String battery){
this.battery = battery;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work() {
String str = "这是一辆使用" + this.getBattery() + "牌电池的电动车。";
return str;
}
}

子类3
public class Tricycle extends NonMotor {
// 在无参构造中实现对轮子属性值进行修改
public Tricycle(){
super.setWheel(3);
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str = "三轮车是一款有" + this.getWheel() + "个轮子的非机动车。";
return str;
}

}

测试类
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 ev = new ElectricVehicle("飞鸽");
System.out.println(ev.work());
System.out.print("三轮车类信息测试:");
Tricycle tricycle = new Tricycle();
System.out.println(tricycle.work());
}
}

写回答

1回答

好帮手慕小蓝

2021-09-18

同学你好,代码运行结果正确,书写规范,做得很好。

有个小建议:通常当父类和子类都定义有属性的时候,建议子类提供能够给父类属性赋值的构造方法。也就是说,同学代码中ElectricVehicle类最好额外提供一个三参数的构造方法,用来给父类属性进行赋值。Tricycle类也是同理。代码如下所示:

public ElectricVehicle(String battery,String brand, String color) {
    super(brand, color);
    this.setBattery(battery);
}
public Tricycle(String brand, String color){
    super(brand, color);
    super.setWheel(3);
}

虽然题目中的演示结果没有对这点做出要求,但还是建议同学提供一下,这样的代码会更加的规范。

祝学习愉快~

0
heijyu
hp>好的!谢谢老师!

h021-09-19
共1条回复

0 学习 · 9886 问题

查看课程