2-16 车辆管理,麻烦老师看看,谢谢。

2-16 车辆管理,麻烦老师看看,谢谢。

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
public class NonMotor {
    // 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
 
    private String label;
    private String color;
    private int wheel;
    private int seat;
    // 公有的get***/set***方法完成属性封装
 
    public String getLabel() {
        return label;
    }
 
    public void setLabel(String label) {
        this.label = label;
    }
 
    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 NonMotor() {
         
    }
    // 双参构造方法,完成对品牌和颜色的赋值
    {
        this.seat = 1;
        this.wheel = 2;
    }
 
    public NonMotor(String Label, String Color) {
        this.setColor(Color);
        this.setLabel(Label);
         
    }
 
    // 四参构造方法,分别对所有属性赋值
    public NonMotor(String Label, String Color, int Wheel, int Seat) {
        this.setColor(Color);
        this.setLabel(Label);
        this.setSeat(Seat);
        this.setWheel(Wheel);
    }
 
    // 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
    public String work() {
        return "这是" this.label + "牌" + color + "颜色的非机动车," "有" + seat + "个座位和"
                this.wheel + "个车轮";
    }
}
public class Bicycle extends NonMotor {
 
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle(String Label,String Color){
super(Label,Color);
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
 
@Override
public String work() {
return "这是" this.getLabel() + "牌" this.getColor() + "颜色的非机动车," "有" this.getSeat() + "个座位和"
this.getWheel() + "个车轮";
}
 
}
public class ElectricVehicle extends NonMotor {
    // 私有属性:电池品牌
    private String baterry;
 
    // 公有的get***/set***方法完成属性封装
    public String getBaterry() {
        return baterry;
    }
 
    public void setBaterry(String baterry) {
        this.baterry = baterry;
    }
 
    public ElectricVehicle(String Battery) {
        this.setBaterry(Battery);
    }
 
    // 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
    @Override
    public String work() {
        // TODO Auto-generated method stub
        return "这是一辆使用" this.baterry + "牌电池的电动车";
    }
 
}
public class Tricycle extends NonMotor {
    // 在无参构造中实现对轮子属性值进行修改
    public Tricycle() {
        super.setSeat(3);
    }
    // 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
 
    @Override
    public String work() {
        // TODO Auto-generated method stub
        return "三轮车是一款有"+this.getSeat()+"个轮子的非机动车";
    }
     
}
public class Test {
public static void main(String[]args){
    NonMotor x=new NonMotor("天马牌","红色",2,1);
    System.out.print("父类信息测试:");
    System.out.println(x.work());
    Bicycle bi=new Bicycle("捷安特","黄色");
    System.out.print("自行车类信息测试:");
    System.out.println(bi.work());
    ElectricVehicle ev=new ElectricVehicle("比亚迪");
    System.out.print("电动车类信息测试:");
    System.out.println(ev.work());
    Tricycle tri=new Tricycle();
    System.out.print("三轮车类信息测试:");
System.out.println(tri.work());
     }
}


正在回答

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

3回答

老铁 没毛病!只不过我好像记得老师说过构造器最好要写到前面哦。还有Bicycle的work方法既然和父类一样,就可以不用重写,直接用父类的就可以了吧。

  • 慕粉2242067667 提问者 #1
    多谢!看视频囫囵吞枣,好多细节没注意哈
    2017-03-28 19:23:32
慕粉2120405385 2017-05-10 18:49:37

老铁,其中有几处错误

这里在set方法中对属性进行赋值,然后 if (this.wheel >= 2 && this.wheel<=4) 中把this去掉就对了 

public int getWheel() {
  return wheel;
 }

 public void setWheel(int wheel) {

 if (wheel >= 2 && wheel<=4) 
   this.wheel = wheel;
   else
   this.wheel = 2;  this.wheel = wheel;
 }

还有就是

public class Tricycle extends NonMotor {

    // 在无参构造中实现对轮子属性值进行修改

    public Tricycle() {

        super.setSeat(3);(这里不是对Seat赋值3,应该是对wheel进行赋值3,应该改为  super.setWheel(3))

    }

    // 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供

 

    @Override

    public String work() {

        // TODO Auto-generated method stub

        return "三轮车是一款有"+this.getSeat()+"个轮子的非机动车";(改为return "三轮车是一款有"+this.getWheel()+"个轮子的非机动车";

如果帮到你,请采纳谢谢


Silent_night 2017-03-30 00:35:19

老铁 没毛病

问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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