1.本实践环节中,重写运行方法“其中**数据由属性提供”具体值得是什么?

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个轮子的非机动车,其中**的数据由属性提供

正在回答

登陆购买课程后可参与讨论,去登陆

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);
    }

祝学习愉快~

  • Ironxi_work 提问者 #1
    老师您说的第二点中,能说一下为什么这样吗 ?重新修改了一下,把子类中类似的有参构造name=name全部注释掉,也完全输出了相同的结果,这是因为调用了父类的无参构造吗?
    2018-01-08 22:10:23
  • Ironxi_work 提问者 #2
    非常感谢!
    2018-01-09 20:48:32
THappy 2018-01-10 10:19:55

代码没有问题~

因为创建自行车对象时传入两个参数,调用自行车类的双参构造,并在其中通过super(name,color);调用父类双参构造。创建电动车对象时传入一个参数,调用电动车类的单参构造,并在其中调用父类无参构造。创建三轮车对象时没有传入参数,调用三轮车类的无参构造,并在其中调用父类无参构造。祝学习愉快~

THappy 2018-01-09 10:05:51

因为wheel和seat属性也是子类继承自父类的属性,与name和color同理可以调用父类的有参构造方法来实现属性的赋值。另外你添加注释以后子类有参构造中还有哪些代码呢?

  • 提问者 Ironxi_work #1
    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 ; } }
    2018-01-09 20:58:58
  • 提问者 Ironxi_work #2
    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; } }
    2018-01-09 20:59:51
  • 提问者 Ironxi_work #3
    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 ; } }
    2018-01-09 21:00:48
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码