⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pinto2.java

📁 java2 primer plus一书源程序
💻 JAVA
字号:
public class Pinto2 extends Car
{
    // State attributes
    public boolean hatchBackDoorOpen = false;

    public Pinto2()
    {
        super( "Pinto",
               50,
               60,
               5,
               "two-tone",
               15,
               5,
               Car.MANUAL );
    }

    public void accelerate()
    {
        // Check to see if we are running or not
        if( running == false )
        {
            return;
        }

        currentSpeed += 4;
        if( currentSpeed > maximumSpeed ) 
        {                       
            currentSpeed = maximumSpeed;

            // The high speed knocked the door open!
            openHatchBack();
        }
    }

    public void decelerate()
    {
        currentSpeed -= 5;
        if( currentSpeed < 0 ) currentSpeed = 0;
    }

    public void rollStart()
    {
        running = true;
    }

    public void pushUpHill()
    {
        System.out.println( "Ouch, this thing is heavy!" );
    }
    
    public void openHatchBack()
    {
        hatchBackDoorOpen = true;
    }
    
    public void closeHatchBack()
    {
        hatchBackDoorOpen = false;
    }

    public boolean isHatchBackDoorOpen()
    {
        return hatchBackDoorOpen;
    }

    public static void main( String[] args )
    {
        Pinto2 p = new Pinto2();
        System.out.println( "My car: " + p );
        p.start();
        if( p.isRunning() == false )
        {
            System.out.println( "Starter failed, let's roll start it!" );
            p.rollStart();
        }
        System.out.println( "Current speed: " + p.getCurrentSpeed() + 
                            ", Hatchback open = " + p.isHatchBackDoorOpen() );
        for( int i=0; i<20; i++ )
        {
            p.accelerate();
            System.out.println( "Current speed: " + p.getCurrentSpeed() + 
                                ", Hatchback open = " + p.isHatchBackDoorOpen() );
        }
        while( p.getCurrentSpeed() > 0 ) 
        {
            p.decelerate();
            System.out.println( "Current speed: " + p.getCurrentSpeed() + 
                                ", Hatchback open = " + p.isHatchBackDoorOpen() );
        }
        p.stop();
        if( p.isHatchBackDoorOpen() )
        {
            System.out.println( "Have to close the hatchback!" );
            p.closeHatchBack();
        }
    }
}

⌨️ 快捷键说明

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