2018年12月12日

课后作业1

在这里插入图片描述

Vehiclel类代码:

public class Vehicle {
    private String brand;
    private String color;
    private double speed;

    public Vehicle(String brand,String color){
        this.brand = brand;
        this.color = color;
        this.speed = 0;
    }

    public Vehicle(){

    }

    public String getBrand() {
        return brand;
    }


    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }


    public double getSpeed() {
        return speed;
    }
    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public void run(double num){
        while(true){

//          用来显示循环间隔,车辆提速间隔时间
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(this.getSpeed()<num){
                System.out.println("车辆正在提速,当前车速为"+this.getSpeed());
            }else{
                break;
            }
            this.setSpeed(this.getSpeed()+10);
        }
        System.out.println(this.getColor()+"色的"+this.getBrand()+"品牌的的汽车以"+this.getSpeed()+"的速度在行驶。");
    }
}

Car类 代码:

class Car extends Vehicle {

    private int loader;

    public int getLoader() {
        return loader;
    }

    public void setLoader(int loader) {
        this.loader = loader;
    }

    public Car(String brand, String color, int loader) {
        super(brand,color);
        this.loader = loader;
    }

     public void run(double num){
        while(true){

//          用来显示循环间隔,车辆提速间隔时间
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(this.getSpeed()<num){
                System.out.println("车辆正在提速,当前车速为"+this.getSpeed());
            }else{
                break;
            }
            this.setSpeed(this.getSpeed()+10);
        }
        System.out.println(this.getColor()+"色的,载着"+this.getLoader()+"人的"+this.getBrand()+"品牌的的汽车以"+this.getSpeed()+"的速度在行驶。");
    }
}

测试类VehicleTest类代码:

import java.util.Scanner;

public class VehicleTest {
    public static void main(String[] args) {
        System.out.println("请输入汽车行驶的速度……");
        Scanner in = new Scanner(System.in);
        double speed = in.nextDouble();

        Vehicle v = new Vehicle("benz", "红");
        v.run(speed);

        Car car = new  Car("honda","红",2);
        car.run(speed);
    }
}

执行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值