请老师帮忙看看,有哪些可以改进的地方
public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String brand;
private String color;
private int wheel;
private int seat;
// 无参构造方法
public NonMotor() {
// System.out.println("NonMotor的无参构造方法");
}
// 双参构造方法,完成对品牌和颜色的赋值
public NonMotor(String brand,String color) {
// System.out.println("NonMotor的双参构造方法");
this.setBrand(brand);
this.setColor(color);
}
// 四参构造方法,分别对所有属性赋值
public NonMotor(String brand,String color,int wheel,int seat) {
// System.out.println("NonMotor的四参构造方法");
this.setBrand(brand);
this.setColor(color);
this.setWheel(wheel);
this.setSeat(seat);
}
// 公有的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 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;
}
}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;
}
}public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String 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;
}
}public class Test {
public static void main(String[] args) {
System.out.print("父类信息测试:");
NonMotor one=new NonMotor("天宇牌","红色",4,2);
System.out.println(one.work());
System.out.print("自行车类信息测试:");
Bicycle two=new Bicycle("捷安特","黄");
System.out.println(two.work());
System.out.print("电动车类信息测试:");
ElectricVehicle three=new ElectricVehicle();
three.setBattery("飞鸽牌");
System.out.println(three.work());
System.out.print("三轮车类信息测试:");
Tricycle four=new Tricycle();
System.out.println(four.work());
}
}public class Tricycle extends NonMotor {
// 在无参构造中实现对轮子属性值进行修改
public Tricycle() {
super.setWheel(3);
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str="三轮车是一款有"+this.getWheel()+"个轮子的非机动车。。";
return str;
}
}1
收起
正在回答
1回答
同学,你好 同学的代码运行是没有问题,有一点建议:在Bicycle中添加一个无参构造,在java中如果没有写构造方法,系统会默认添加一个构造方法,但是如果写了一个带参构造,系统就不会默认添加了,但是在Bicycle类中,代码有带参构造的方法并没有写无参构造,如果这时有一个类继承Bicycle,这个类写了无参构造后,会默认调用Bicycle中的无参构造方法,但是Bicycle并没有写无参构造,这时候程序就会报错,所以一个类不管有有没有用到无参构造都建议写上!
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星