老师你看一下这个代码有问题不
public class Test3 { public static void main(String[] args){ NonMotor non=new NonMotor("天宇","红色",2,4); System.out.print("父类测试信息: "); System.out.println(non.info()); Bicycle one=new Bicycle(); System.out.print("自行车测试信息: "); System.out.println(one.info()); ElectricVehicle two=new ElectricVehicle("电动车","飞鸽"); System.out.print("电动车测试信息: "); System.out.println(two.info()); Tricycle three=new Tricycle(); System.out.print("三轮车测试信息: "); System.out.println(three.info()); } } public class NonMotor { private String cycleName; private int tyreNumber; private String cycleColor; private int seatNumber; public String getCycleName() { return cycleName; } public void setCycleName(String cycleName) { this.cycleName = cycleName; } public int getTyreNumber() { return tyreNumber; } public void setTyreNumber(int tyreNumber) { this.tyreNumber = tyreNumber; } public String getCycleColor() { return cycleColor; } public void setCycleColor(String cycleColor) { this.cycleColor = cycleColor; } public int getSeatNumber() { return seatNumber; } public void setSeatNumber(int seatNumber) { this.seatNumber = seatNumber; } public NonMotor() { } // 带参构造,实现对品牌,颜色的传值 public NonMotor(String cycleName, String cycleColor) { this.setCycleColor(cycleColor); this.setCycleName(cycleName); } // 带参构造,实现对所有属性的传值 public NonMotor(String cycleName, String cycleColor, int seatNum, int tyreNum) { this.setCycleColor(cycleColor); this.setCycleName(cycleName); this.setSeatNumber(seatNum); this.setTyreNumber(tyreNum); } // 运行的方法 public String info() { // 这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车 String str = "这是一辆" + this.getCycleColor() + "颜色的," + this.getCycleName() + "牌的非机动车," + "有" + this.getTyreNumber() + "个轮子,有" + this.getSeatNumber() + "个座椅"; return str; } } public class Bicycle extends NonMotor { //可以采用两种赋值的方法,一是无参构造中采用super调用父类带参构造,二是直接采用带参构造完成 //子类无参构造 public Bicycle(){ super("捷安特","黄色"); } public Bicycle(String cycleName,String cycleColor){ super(cycleName,cycleColor); } public String info(){ String str="这是一辆"+this.getCycleColor()+"的,"+this.getCycleName()+"牌的自行车"; return str; } } public class ElectricVehicle extends NonMotor { private String battery; public String getBattery() { return battery; } public void setBattery(String battery) { this.battery = battery; } //无参构造方法 public ElectricVehicle(){ } //带参构造方法,实现对电池,车牌的传值,会调用父类的无参构造方法 public ElectricVehicle(String cycleName,String battery){ this.setBattery(battery); this.setCycleName(cycleName); } //对父类方法的重写 public String info(){ String str="这是一辆使用"+this.getBattery()+"牌电池的"+this.getCycleName(); return str; } } public class Tricycle extends NonMotor { // 无参构造方法,通过super,引用父类的带参构造方法,实现对三轮车属性的传值 public Tricycle() { super("三轮车", "黄色", 1, 3); } public String info() { String str = this.getCycleName() + "是一款有" + this.getTyreNumber() + "个轮子的非机动车"; return str; } }
这个作业的代码有什么要改正的没有老师?
28
收起
正在回答
2回答
有一点不太对,Tricycle类的无参构造方法中对三轮车的轮子属性进行赋值应该用this.setTyreNumber(x);实现,其中x为设置的轮子数量。
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星