设计一个用于交通工具管理的程序,具体实现以下要求:
(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)从键盘输入车辆的信息(要求包括小汽车、卡车和客货两用车),保存在某类集合(具体集合类自己选择)中,应用多态编程输出所有车的信息,计算所有车的总价格并输出。
一个实现,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);
}
}