Problem A: 实验三:编写汽车类

Problem Description

1.实验目的
    (1) 熟悉类的创建方法
    (2) 掌握对象的声明与创建
    (3) 能利用面向对象的思想解决一般问题
 
2.实验内容 
   编写一个java程序,设计一个汽车类Vehicle,包含的属性有车轮的个数wheels和车重weight。小汽车类Car是Vehicle的子类,包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。

3.实验要求
   补充完整下面的代码

  public class Main{
    public static void main(String[] args){
        Vehicle v=new Vehicle(8,10.00);
        smallCar c=new smallCar(6);
        Truck t=new Truck(10);
        v.disMessage();
        c.disM();
        t.disM2();
        t.disM3();
   }
}
    // 你的代码

Sample Output

The number of wheels in this car is 8, weight is 10.0
This car can carry 6 persons
The load of this truck is 10
The number of wheels in this truck is 8, weight is 10.0, can carry 6 persons, load of this truck is 10

 我的代码:

public class Main{
    public static void main(String[] args){
        Vehicle v=new Vehicle(8,10.00);
        smallCar c = new smallCar(6);
        Truck t=new Truck(10);
        v.disMessage();
        c.disM();
        t.disM2();
        t.disM3();
    }
}
// 你的代码
class  Vehicle{
    //车轮的个数
    static int wheels;
    //车重
    static double weight;
    public Vehicle(){}
    public Vehicle(int i, double v) {
        wheels = i;
        weight = v;
    }


    public void disMessage() {
        System.out.println("The number of wheels in this car is "+ wheels + ", weight is " + weight);
    }
}

class  smallCar extends Vehicle{

    //载人数
    static int loader;

    public smallCar() {
    }

    public void disM(){
        System.out.println("This car can carry "+ loader +" persons");
    }

    public smallCar(int i){
        super();
        loader = i;
    }

    public int getWheels(){
        return wheels;
    }


    public double getWeight(){
        return  weight;
    }
}


class Truck extends smallCar{

    //载重量
    static int payload;
    public Truck(int i){
        super();
        payload = i;
    }

    public void disM2(){
        System.out.println("The load of this truck is " + payload);
    }


    public void disM3(){
        System.out.println("The number of wheels in this truck is " + wheels + ", weight is " + weight+
                ", can " +
                "carry " + loader + " persons," +
                " " +
                "load of this truck is " + payload);
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小木苓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值