期末考试复习

时间:2012-12-16 15:53:56

标签: java polymorphism

我很快就会参加我的java课程,我希望能得到一个问题的帮助。这是关于以下示例代码:

package test;

public class Exam2Code
{

    public static void main ( String[ ] args )
    {
        Lot parkingLot = new Lot();

        Car chevy = new Car( );
        Car camry = new Car( );
        MotorCycle harley = new MotorCycle( 3 );
        MotorCycle honda = new MotorCycle( );

        parkingLot.park ( chevy );
        parkingLot.park ( honda );
        parkingLot.park ( harley );
        parkingLot.park ( camry );

        System.out.println( parkingLot.toString( ) );
        System.out.println(chevy.getClass());
        System.out.println(camry.getClass());
        System.out.println(harley.getClass());
        System.out.println(honda.getClass());
    }


} 

package test;

public class Vehicle
{

    private int nrWheels;

    public Vehicle( )
        { this( 4 ); }
    public Vehicle ( int nrWheels )
        { setWheels( nrWheels); }
    public String toString (  )
        { return "Vehicle with " + getWheels()
                 + " wheels"; }
    public int getWheels ( )
        { return nrWheels; }
    public void setWheels ( int wheels )
        { nrWheels = wheels; }
}

package test;

public class MotorCycle extends Vehicle
{

    public MotorCycle ( )
        { this( 2 ); }
    public MotorCycle( int wheels )
        { super( wheels ); }


}

package test;

public class Car extends Vehicle
{

    public Car ( )
        { super( 4 ); }
    public String toString( )
    {
        return "Car with " + getWheels( ) + " wheels";
    }
}

package test;

public class Lot
{

    private final static int MAX_VEHICLES = 20;
    private int nrVehicles;
    private Vehicle [] vehicles;

    public Lot (  )
    {
        nrVehicles = 0;
        vehicles = new Vehicle[MAX_VEHICLES];
    }

    public int nrParked (  )
        { return nrVehicles; }
    public void park ( Vehicle v )
        { vehicles[ nrVehicles++ ] = v; }
    public int totalWheels ( )
    {
        int nrWheels = 0;
        for (int v = 0; v < nrVehicles; v++ )
                nrWheels += vehicles[ v ].getWheels( );
        return nrWheels;
    }
    public String toString( )
    {
        String s = "";

        for (Vehicle v : vehicles){
                if(v != null){
                        s += v.toString( ) + "\n";
        }
    }
        return s;
    }
}

问题是“识别Lot的park方法中常见的编码错误。你如何纠正这个编码错误?”。我不知道错误是什么。我原来的答案是它是一个复制构造函数,并使用clone()方法来纠正它。但我甚至不确定它是否是一个复制构造函数。我使用getClass()检查了他们的类,它们似乎都是正确的类型。如果有人能帮我解决这个问题,我将不胜感激。谢谢大家!

编辑:添加了代码。

3 个答案:

答案 0 :(得分:4)

  

确定Lot的park方法中的常见编码错误。您如何更正此编码错误?

以下是相关的代码:

private int nrVehicles;
private Vehicle [] vehicles;

public Lot (  )
{
    nrVehicles = 0;
    vehicles = new Vehicle[MAX_VEHICLES];
}

public void park ( Vehicle v ) { 
    vehicles[ nrVehicles++ ] = v; 
}

我在这里看到的唯一“问题”是当你停放超过nrVehicles辆汽车时缺少范围检查(这是由JVM隐式完成的,但我想你的老师并不满意),所以:

public void park ( Vehicle v ) { 
    if(nrVehicles < MAX_VEHICLES) {
      vehicles[nrVehicles++] = v; 
    } else {
      throw new IllegalStateException("Lot full, go find another one!");
    }
}

答案 1 :(得分:1)

另外一个有趣的(真实世界)错误是,同一车辆可能停在该地段的几个位置。

答案 2 :(得分:0)

Vehicle  chevy = new Car();
Vehicle camry = new Car();
Vehicle harley = new MotorCycle( 3 );
Vehicle honda = new MotorCycle( );