设计一个用于交通工具管理的程序,具体实现以下要求:
(1)定义一个名为Vehicles(交通工具)的抽象类,类中包含的实例字段有:brand(品牌名称)、color(颜色)、price(价格),包含方法showInfo(在控制台显示品牌名称、颜色与价格),并编写构造方法初始化其实例字段。
(2)定义Car(小汽车)类继承于Vehicles类,增加int型实例字段seats(座位数),重写showInfo方法(在控制台显示小汽车信息),并编写构造方法。
(3)编写Truck(卡车)类继承于Vehicles类,增加float型实例字段load(载重),重写showInfo方法(在控制台显示卡车的信息),并编写构造方法。
(4)编写Van(客货两用车)类继承于Vehicles类,增加int型实例字段seats(座位数量)和float型实例字段load(载重),重写showInfo方法(在控制台显示客货两用车的信息),并编写构造方法。
(5)从键盘输入车辆的信息(要求包括小汽车、卡车和客货两用车),保存在某类集合(具体集合类自己选择)中,应用多态编程输出所有车的信息,计算所有车的总价格并输出。
![](https://profile-avatar.csdnimg.cn/d7e5accb9de840049b97bc4601e98e33_qq_63584043.jpg!4)
java实验-设计一个用于交通工具管理的程序
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- a5156520 2022-06-02 16:40关注
一个实现,showInfo的输出信息可按自己需求调整,供参考:
import java.util.ArrayList; import java.util.Scanner; public class CarTest { public static void main(String[] args) { // TODO Auto-generated method stub //把交通工具类作为泛型,方便添加其子类,及下面的遍历 ArrayList<Vehicles> cars = new ArrayList<Vehicles>(); String brand = null; String color = null; double price = 0; int seats = 0; float load = 0; //从输入获取小车信息 System.out.println("请输入小汽车的信息:"); Scanner sc = new Scanner(System.in); System.out.print("品牌名称:"); brand = sc.next(); System.out.print("颜色:"); color = sc.next(); System.out.print("座位数:"); seats = sc.nextInt(); System.out.print("价格:"); price = sc.nextDouble(); Car car = new Car(brand,color,price,seats); cars.add(car); //从输入获取卡车信息 System.out.println("请输入卡车的信息:"); sc = new Scanner(System.in); System.out.print("品牌名称:"); brand = sc.next(); System.out.print("颜色:"); color = sc.next(); System.out.print("载重:"); load = sc.nextFloat(); System.out.print("价格:"); price = sc.nextDouble(); Truck truck = new Truck(brand,color,price,load); cars.add(truck); //从输入获取客货两用车信息 System.out.println("请输入客货两用车的信息:"); sc = new Scanner(System.in); System.out.print("品牌名称:"); brand = sc.next(); System.out.print("颜色:"); color = sc.next(); System.out.print("座位数:"); seats = sc.nextInt(); System.out.print("价格:"); price = sc.nextDouble(); Van van = new Van(brand,color,price,seats); cars.add(van); //用for-each语法遍历ArrayList集合 for(Vehicles v:cars) { v.showInfo(); } } } //交通工具抽象类 abstract class Vehicles{ public String brand; public String color; public double price; public abstract void showInfo(); public Vehicles(String brand,String color, double price) { this.brand = brand; this.color = color; this.price = price; } } //小汽车类 class Car extends Vehicles{ public int seats; public Car(String brand, String color, double price,int seats) { super(brand, color, price); this.seats = seats; // TODO Auto-generated constructor stub } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println("Car information :\n" +" brand: "+this.brand +" color: "+this.color +" seats: "+this.seats +" price: "+this.price); } } //卡车类 class Truck extends Vehicles{ public float load; public Truck(String brand, String color, double price,float load) { super(brand, color, price); this.load = load; // TODO Auto-generated constructor stub } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println("Truck information :\n" +" brand: "+this.brand +" color: "+this.color +" load: "+this.load +" price: "+this.price); } } //客货两用车类 class Van extends Vehicles{ public int seats; public Van(String brand, String color, double price,int seats) { super(brand, color, price); this.seats = seats; // TODO Auto-generated constructor stub } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println("Van information :\n" +" brand: "+this.brand +" color: "+this.color +" seats: "+this.seats +" price: "+this.price); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用
悬赏问题
- ¥15 metadata提取的PDF元数据,如何转换为一个Excel
- ¥15 关于arduino编程toCharArray()函数的使用
- ¥100 vc++混合CEF采用CLR方式编译报错
- ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
- ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
- ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
- ¥50 如何openEuler 22.03上安装配置drbd
- ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
- ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
- ¥15 Windows11, backspace, enter, space键失灵