newcar.java

来自「java2 primer plus一书源程序」· Java 代码 · 共 89 行

JAVA
89
字号
public abstract class NewCar 
{
    // Transmission types
    public static final int AUTOMATIC = 0;
    public static final int TIPTRONIC = 1;
    public static final int MANUAL = 2;

    // Attributes
    protected String typeOfCar;
    protected int horsePower;
    protected int maximumSpeed;
    protected int numberOfDoors;
    protected String paint;
    protected int gasCapacity;
    protected int oilCapacity;
    protected int transmission;

    // State attributes
    protected boolean running = false;
    protected int currentSpeed;
    protected int currentGas;
    protected int currentOil;

    public NewCar( String typeOfCar,
                   int horsePower,
                   int maximumSpeed,
                   int numberOfDoors,
                   String paint,
                   int gasCapacity,
                   int oilCapacity,
                   int transmission )
    {
        this.typeOfCar = typeOfCar;
        this.horsePower = horsePower;
        this.maximumSpeed = maximumSpeed;
        this.numberOfDoors = numberOfDoors;
        this.paint = paint;
        this.gasCapacity = gasCapacity;
        this.oilCapacity = oilCapacity;
        this.transmission = transmission;
    }

    public void start() throws CarException
    {
        if( this.running == true )
        {
            CarException e = new CarException( "Car is already running --- nasty grinding noise!" );
            e.setEngineRunning( true );
            throw e;
        }
        running = true;
    }

    public void stop() throws CarException
    {
        if( this.running == false )
        {
            CarException e = new CarException( "Car is not running!" );
            e.setEngineRunning( false );
            throw e;
        }
        running = false;
    }

    public boolean isRunning()
    {
        return running;
    }

    public abstract void accelerate() throws CarException;

    public abstract void decelerate() throws CarException;

    public abstract void tuneUp();

    public abstract void changeOil();

    public int getCurrentSpeed()
    {
        return currentSpeed;
    }

    public String toString()
    {
        return typeOfCar; 
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?