1.本实践环节中,重写运行方法“其中**数据由属性提供”具体值得是什么?
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | 1 .代码成功运行,但觉得不太完美,有那些需要改善的地方? 2 .重写运行方法的问题,其中“其中**数据由属性提供”以三轮车为例是要输出除轮子之外的对象外其他对象比如座椅品牌是由属性提供?还是的是什么? 3 .代码如下: package com.imooc.transportation.imoo; public class NonMotor { // 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个) private String name; private String color; private int wheel= 2 ; private int seat= 1 ; // 无参构造方法 public NonMotor() { super (); } // 双参构造方法,完成对品牌和颜色的赋值 public NonMotor(String name, String color) { this .name = name; this .color = color; } // 四参构造方法,分别对所有属性赋值 public NonMotor(String name, String color, int wheel, int seat) { super (); this .name = name; this .color = color; this .wheel = wheel; this .seat = seat; } // 公有的get***/set***方法完成属性封装 public String getName() { return name; } public void setName(String name) { this .name = name; } 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 .getWheel()+ "个轮子," + "有" + this .getSeat()+ "个座椅的非机动车," + "其中" + "的数据由属性提供" ; return str ; } } package com.imooc.transportation.imoo; public class Bicycle extends NonMotor { // 在构造方法中调用父类多参构造,完成属性赋值 public Bicycle() { super (); } public Bicycle(String name, String color) { super (name,color); name =name; color = color; } public Bicycle(String name, String color, int wheel, int seat) { super (name,color,wheel,seat); name =name; color = color; wheel = wheel; seat = seat; } // 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供 public String work() { String str= "这是一辆" + this .getColor()+ "颜色的," + this .getName()+ "的自行车" ; return str ; } } package com.imooc.transportation.imoo; public class ElectricVehicle extends NonMotor { // 私有属性:电池品牌 private String eletricBehicleName; // 公有的get***/set***方法完成属性封装 public ElectricVehicle() { super (); } public ElectricVehicle(String name, String color) { super (name, color); name = name; color = color; } public ElectricVehicle(String name, String color, int wheel, int seat) { super (name, color, wheel, seat); name = name; color = color; wheel = wheel; seat = seat; } public ElectricVehicle(String eletricBehicleName) { super (); this .eletricBehicleName = eletricBehicleName; } public String getEletricBehicleName() { return eletricBehicleName; } public void setEletricBehicleName(String eletricBehicleName) { this .eletricBehicleName = eletricBehicleName; } // 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供 public String work() { String str = "这是一辆使用" + this .getEletricBehicleName() + "牌电池的电动车," + "其中" + "**的数据由属性提供" ; return str; } } package com.imooc.transportation.imoo; public class Tricycle extends NonMotor{ // 在无参构造中实现对轮子属性值进行修改 public Tricycle() { this .setWheel( 3 ); } public Tricycle(String name, String color) { super (name,color); name =name; color = color; } public Tricycle(String name, String color, int wheel, int seat) { super (name,color,wheel,seat); name =name; color = color; wheel = 3 ; seat = seat; } // 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供 public String work() { String str = "三轮车是一款有" + this .getWheel()+ "个轮子的非机动车," + "其中**的数据由属性提供" ; return str ; } } package com.imooc.transportionTest; import com.imooc.transportation.imoo.*; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub NonMotor one= new NonMotor( "国婷" , "黄色" ); System.out.println(one.work()); Bicycle two= new Bicycle( "捷安特" , "黄" ); System.out.println(two.work()); ElectricVehicle three = new ElectricVehicle( "福特电池" ); System.out.println(three.work()); Tricycle four = new Tricycle(); System.out.println(four.work()); } } |
1 2 3 4 5 | 输出结果: 这是一辆黄色颜色的非机动车,有 2 个轮子,有 1 个座椅的非机动车,其中的数据由属性提供 这是一辆黄颜色的,捷安特的自行车 这是一辆使用福特电池牌电池的电动车,其中**的数据由属性提供 三轮车是一款有 3 个轮子的非机动车,其中**的数据由属性提供 |
36
收起
正在回答
3回答
1、其中**的数据由属性提供:这句话是对描述信息里的“**”做说明,不需要输出的。
2、你的代码整体完成的不错,给你提出一个意见:NonMotor的子类中,构造方法调用super后,就不需要name=name类似的这些代码了,例如:直接写为这样就可以了
1 2 3 | public Bicycle(String name, String color, int wheel, int seat) { super (name,color,wheel,seat); } |
祝学习愉快~
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧