麻烦老师看看代码有什么需要改进的地方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | package Test; import Vehicle.*; public class TestDemo { public static void main(String[] args) { NonMotor nm= new NonMotor( "天宇" , "红色" , 2 , 4 ); System.out.println(nm.work()); Bicycle b= new Bicycle( "捷安特" , "黄" ); System.out.println(b.str()); ElectricVehicle ev= new ElectricVehicle( "飞鸽" ); System.out.println(ev.str()); TriCycle tc = new TriCycle(); System.out.println(tc.str()); } } package Vehicle; public class NonMotor { // 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个) private String brand; private String color; private int wheels; private int seat; // 无参构造方法 public NonMotor(){ this .setSeat( 1 ); this .setWheels( 2 ); } // 双参构造方法,完成对品牌和颜色的赋值 public NonMotor(String brand,String color) { this .setBrand(brand); this .setColor(color); this .setSeat( 1 ); this .setWheels( 2 ); } // 四参构造方法,分别对所有属性赋值 public NonMotor(String brand,String color, int seat, int wheels) { this .setBrand(brand); this .setColor(color); this .setSeat(seat); this .setWheels(wheels); } // 公有的get***/set***方法完成属性封装 // 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供 public String work() { return "这是一辆" + this .getColor()+ "颜色的," + this .getBrand()+ "牌的非机动车,有" + this .getWheels()+ "个轮子,有" + this .getSeat()+ "个座椅的非机动车。其中**的数据由属性提供" ; } 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 getWheels() { return wheels; } public void setWheels( int wheels) { this .wheels = wheels; } public int getSeat() { return seat; } public void setSeat( int seat) { this .seat = seat; } } package Vehicle; public class Bicycle extends NonMotor { public Bicycle() { super (); } public Bicycle(String brand,String color) { super (brand,color); } public String str() { return "这是一辆" + this .getColor()+ "颜色的," + this .getBrand()+ "牌的自行车。其中**的数据由属性提供" ; } } package Vehicle; public class ElectricVehicle extends NonMotor { private String batteryBrand; public ElectricVehicle(String batteryBrand) { this .setBatteryBrand(batteryBrand); } public String str() { return "这是一辆使用" + this .getBatteryBrand()+ "牌电池的电动车。其中**的数据由属性提供" ; } public String getBatteryBrand() { return batteryBrand; } public void setBatteryBrand(String batteryBrand) { this .batteryBrand = batteryBrand; } } package Vehicle; public class TriCycle extends NonMotor{ public TriCycle() { super .setWheels( 3 ); } public String str() { return "三轮车是一款有" + this .getWheels()+ "个轮子的非机动车。其中**的数据由属性提供" ; } } |
0
收起
正在回答
1回答
运行效果符合题目要求,但是根据任务分析,自行车、电动车以及三轮车应该重写父类中的work()方法。而不是新创建;建议同学根据任务分析进行修改哟~
祝学习愉快!
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧