编程练习:某公司要开发“XX车行管理系统”,请使用面向对象的思想,设计自定义类描述自行车、电动车和三轮车。
程序参考运行效果图如下:
父类信息测试:这是一辆红颜色的,天宇牌的非机动车,有4个轮子,有2个座椅的非机动车
自行车类信息测试:这是一辆捷安特颜色的,黄色牌的自行车。
电动车类信息测试:这是一辆使用飞鸽牌电池的电动车
三轮车类信息测试:三轮车是一款有3个轮子的非机动车。
任务分析;
第一步:分析自行车、电动车和三轮车的共性:
都是非机动车,具有非机动车的基本特征
都有运行的方法
第二步:根据共性,定义非机动车
属性:品牌、颜色、轮子(默认2个)、座椅(默认 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
| package me.feihong.car;
public class NonMotor { 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; }
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; }
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) { 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());
}
}
|