XX车行管理系统

编程练习:某公司要开发“XX车行管理系统”,请使用面向对象的思想,设计自定义类描述自行车、电动车和三轮车。

程序参考运行效果图如下:

父类信息测试:这是一辆红颜色的,天宇牌的非机动车,有4个轮子,有2个座椅的非机动车
自行车类信息测试:这是一辆捷安特颜色的,黄色牌的自行车。
电动车类信息测试:这是一辆使用飞鸽牌电池的电动车
三轮车类信息测试:三轮车是一款有3个轮子的非机动车。

任务分析;

第一步:分析自行车、电动车和三轮车的共性:

  1. 都是非机动车,具有非机动车的基本特征

  2. 都有运行的方法

第二步:根据共性,定义非机动车

属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)

方法:

  1. 编写无参构造方法、双参构造方法和四参构造方法,其中,在双参构造方法中,完成对品牌和颜色的赋值;在四参构造方法中,完成对所有属性的赋值

  2. 编写运行的方法,描述内容为:这是一辆颜色的,牌的非机动车,有个轮子,有个座椅的非机动车。其中**的数据由属性提供

第三步:定义自行车、电动车和三轮车分别继承自行车类,要求:

  • 自行车类:
  1. 在构造方法中调用父类多参构造,完成属性赋值
  2. 重写运行方法,描述内容为:这是一辆颜色的,牌的自行车。其中**的数据由属性提供
  • 电动车:
  1. 增加“电池品牌”属性
  2. 重写运行方法,描述内容为:这是一辆使用牌电池的电动车。其中的数据由属性提供
  • 三轮车:
  1. 在无参构造中实现对轮子属性值进行修改
  2. 重写运行方法,描述内容为:三轮车是一款有个轮子的非机动车。其中的数据由属性提供

代码:

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
package me.feihong.car;

public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String name;
private String color;
private int lunzi=2;
private int seat=1;

// 无参构造方法
public NonMotor() {

}

// 双参构造方法,完成对品牌和颜色的赋值
public NonMotor(String name,String color) {
this.name=name;
this.color=color;
}

// 四参构造方法,分别对所有属性赋值
public NonMotor(String name,String color,int lunzi,int seat) {
this.name=name;
this.color=color;
this.lunzi=lunzi;
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 getLunzi() {
return lunzi;
}

public void setLunzi(int lunzi) {
this.lunzi = lunzi;
}

public int getSeat() {
return seat;
}

public void setSeat(int seat) {
this.seat = seat;
}

// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
public String work() {
String str="这是一辆"+getColor()+"颜色的,"+getName()+"牌的非机动车,有"+getLunzi()+"个轮子,有"+getSeat()+"个座椅的非机动车";
return str;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package me.feihong.car;

public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle() {
super("黄色","捷安特");
}

// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str="这是一辆"+getColor()+"颜色的,"+getName()+"牌的自行车。";
return str;
}

}
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
package me.feihong.car;

public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String battery;
//无参构造方法
public ElectricVehicle() {

}

//带参构造方法
public ElectricVehicle(String battery) {
this.battery=battery;
}
// 公有的get***/set***方法完成属性封装
public String getBattery() {
return battery;
}

public void setBattery(String battery) {
this.battery = battery;
}

// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work() {
String str="这是一辆使用"+getBattery()+"牌电池的电动车";
return str;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package me.feihong.car;

public class Tricycle extends NonMotor {
// 在无参构造中实现对轮子属性值进行修改
public Tricycle() {
setLunzi(3);
}

// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str="三轮车是一款有"+getLunzi()+"个轮子的非机动车。";
return str;
}

}
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
package me.feihong.car;

public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("父类信息测试:");
NonMotor motor=new NonMotor("天宇","红",4,2);
System.out.println(motor.work());

System.out.print("自行车类信息测试:");
Bicycle bike=new Bicycle();
System.out.println(bike.work());

System.out.print("电动车类信息测试:");
ElectricVehicle evehicle=new ElectricVehicle("飞鸽");
System.out.println(evehicle.work());

System.out.print("三轮车类信息测试:");
Tricycle tcycle=new Tricycle();
System.out.println(tcycle.work());

}

}
-------------本文结束感谢您的阅读-------------