编写一个制造各种车辆的程序

包含三个类,具体要求如下:
(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();
    }
}

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值