1.本实践环节中,重写运行方法“其中**数据由属性提供”具体值得是什么?
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());
}
}输出结果: 这是一辆黄色颜色的非机动车,有2个轮子,有1个座椅的非机动车,其中的数据由属性提供 这是一辆黄颜色的,捷安特的自行车 这是一辆使用福特电池牌电池的电动车,其中**的数据由属性提供 三轮车是一款有3个轮子的非机动车,其中**的数据由属性提供
36
收起
正在回答
3回答
1、其中**的数据由属性提供:这句话是对描述信息里的“**”做说明,不需要输出的。
2、你的代码整体完成的不错,给你提出一个意见:NonMotor的子类中,构造方法调用super后,就不需要name=name类似的这些代码了,例如:直接写为这样就可以了
public Bicycle(String name, String color, int wheel, int seat) {
super(name,color,wheel,seat);
}祝学习愉快~
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星