编程练习:某公司要开发“XX车行管理系统”,请使用面向对象的思想,设计自定义类描述自行车、电动车和三轮车。
程序参考运行效果图如下:
父类信息测试:这是一辆红颜色的,天宇牌的非机动车,有4个轮子,有2个座椅的非机动车
自行车类信息测试:这是一辆捷安特颜色的,黄色牌的自行车。
电动车类信息测试:这是一辆使用飞鸽牌电池的电动车
三轮车类信息测试:三轮车是一款有3个轮子的非机动车。
任务分析;
第一步:分析自行车、电动车和三轮车的共性:
- 都是非机动车,具有非机动车的基本特征
- 都有运行的方法
第二步:根据共性,定义非机动车
属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
方法:
- 编写无参构造方法、双参构造方法和四参构造方法,其中,在双参构造方法中,完成对品牌和颜色的赋值;在四参构造方法中,完成对所有属性的赋值
- 编写运行的方法,描述内容为:这是一辆颜色的,牌的非机动车,有个轮子,有个座椅的非机动车。其中**的数据由属性提供
第三步:定义自行车、电动车和三轮车分别继承自行车类,要求:
- 自行车类:
- 在构造方法中调用父类多参构造,完成属性赋值
- 重写运行方法,描述内容为:这是一辆颜色的,牌的自行车。其中**的数据由属性提供
- 电动车:
- 增加“电池品牌”属性
- 重写运行方法,描述内容为:这是一辆使用牌电池的电动车。其中的数据由属性提供
- 三轮车:
- 在无参构造中实现对轮子属性值进行修改
- 重写运行方法,描述内容为:三轮车是一款有个轮子的非机动车。其中的数据由属性提供
代码:
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;
}
}
package me.feihong.car;
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle() {
super("黄色","捷安特");
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str="这是一辆"+getColor()+"颜色的,"+getName()+"牌的自行车。";
return str;
}
}
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;
}
}
package me.feihong.car;
public class Tricycle extends NonMotor {
// 在无参构造中实现对轮子属性值进行修改
public Tricycle() {
setLunzi(3);
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str="三轮车是一款有"+getLunzi()+"个轮子的非机动车。";
return str;
}
}
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());
}
}
版权属于:Feihong
本文链接:https://www.feihong.me/archives/46.html
转载时须注明出处及本声明