包含三个类,具体要求如下:
(1)基类Vehicle,包含轮子数和汽车自身重量两个属性,一个两参数的构造方法,一个显示汽车信息的方法;
(2)小轿车类Car,增加载客数属性,重写构造方法和显示车辆信息的成员方法;
(3)卡车类Truck,增加载客数和载货量属性,重写构造方法和显示车辆信息的成员方法;
(4)主程序类,要求输入各种车辆的信息,并在控制台输出各种车辆信息。
package com.fm.demo;
//Vehicle 类
public class Vehicle {
public int num;
public double weight;
public Vehicle(){
}
public Vehicle(int num,double weight){
this.num=num;
this.weight=weight;
}
public void show(){
System.out.println("汽车:");
System.out.println("轮子数:"+num+"个");
System.out.println("自身重量:"+weight+"吨");
}
}
//Car小轿车类
class Car extends Vehicle{
public int pernum;
public Car(int num,double weight,int pernum){
super(num,weight);
this.pernum =pernum;
}
public void show(){
System.out.println("小轿车:");
System.out.println("轮子数:"+num+"个");
System.out.println("自身重量:"+weight+"吨");
System.out.println("额定乘客数:"+ pernum +"人");
}
}
//Truck卡车类
class Truck extends Vehicle{
int pernum;
double perweight;
public Truck(int num,double weight,int pernum,double perweight){
super(num,weight);
this.perweight=perweight;
this.pernum=pernum;
}
public void show(){
System.out.println("卡车:");
System.out.println("轮子数:"+num+"个");
System.out.println("自身重量:"+weight+"吨");
System.out.println("额定乘客数"+pernum+"人");
System.out.println("载重量"+perweight+"吨");
}
}
//测试类
import java.util.Scanner;
public class TestCar {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Vehicle vehicle=new Vehicle(sc.nextInt(),sc.nextDouble());
vehicle.show();
Car car=new Car(sc.nextInt(),sc.nextDouble(),sc.nextInt());
car.show();
Truck truck=new Truck(sc.nextInt(),sc.nextDouble(),sc.nextInt(),sc.nextDouble());
truck.show();
}
}