麻烦老师看看代码有什么需要改进的地方

麻烦老师看看代码有什么需要改进的地方

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()+"个轮子的非机动车。其中**的数据由属性提供";
    }
}


正在回答

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

1回答

运行效果符合题目要求,但是根据任务分析,自行车、电动车以及三轮车应该重写父类中的work()方法。而不是新创建;建议同学根据任务分析进行修改哟~

祝学习愉快!

  • 慕侠3717263 提问者 #1
    嗯嗯,这里就是把方法名改为work就算是方法重写了吧?
    2019-01-20 21:06:30
  • 一叶知秋519 回复 提问者 慕侠3717263 #2
    嗯,是的~ 祝学习愉快!
    2019-01-21 09:55:10
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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