马对象继承

时间:2013-10-02 19:13:04

标签: java inheritance methods junit

我有一个类如下所示,我必须使Horse对象不接受不是Perso对象的驱动程序,并且它继承了Vehicle类,并且不能拥有不属于它自己的引擎。我在我的代码下发布的Junit测试中失败了test4,test1和test2。我相信问题在于我如何处理集引擎并专门设置驱动程序方法。在这种情况下,我需要一种特定的方法来设置方法吗?

horse.java

public class Horse extends Vehicle implements Engine {
public Horse(Driver driver){
super(driver, null);
super.setEngine(this);
}
//@override
public void setEngine(Engine engine){
if(engine == null){
    super.setEngine(this);
}
else if(engine instanceof Horse){
    super.setEngine(engine);
}
else{
    throw new IllegalStateException();}
}

//@override
public void setDriver(Driver driver){
    if(driver instanceof Person){
        super.setDriver(this);
}
    else{
        throw new IllegalStateException();
    }
}
    //@override
    public int getForce(){
        return 746;
    }
    public boolean equals(Object other)
    {
    if(other instanceof Horse){
        if(((Horse)other).getForce() == this.getForce()){
            return true;
        }
        }
        return false;
    }

    public int compareTo(Engine o) {
        int force = this.getForce() - o.getForce();
        return force;

    }
}

Vehicle.java

abstract class Vehicle{
Engine engine;
Driver driver;
public Vehicle(Driver driver, Engine engine){
}
public Engine getEngine(){
    return engine;
}
public void setEngine(Engine aEngine){
    engine = aEngine;
}
public Driver getDriver(){
return driver;
}
public  void setDriver(Driver aDriver){
driver = aDriver;
}

public boolean equals(Object other)
{
if(other instanceof Vehicle){
if(((Vehicle)other).getEngine().equals(this.getEngine())){
    return true;
}
}
return false;
}
public int compareTo(Engine o) {
int force = o.getForce();
return force;

}
}

Engine.java

public interface Engine extends Comparable<Engine>{

int compareTo(Engine o);
public int getForce();

} 

junit test

import static org.junit.Assert.*;

import org.junit.Test;

public class HorseTest {

@Test
public void test0_HorseIsAVehicleAndAnEngine() {
    Horse horse = new Horse( new Person( 55 ) { } );
    assertTrue( "Incorrect result", horse instanceof Vehicle );
    assertTrue( "Incorrect result", horse instanceof Engine  );
}
@Test(expected=IllegalArgumentException.class)
public void test1_HorsesCannotBeRiddenByAnyDriverOtherThanPeople_NEW() {
    new Horse( new Driver() { } );
}
@Test(expected=IllegalArgumentException.class)
public void test2_HorsesCannotBeRiddenByAnyDriverOtherThanPeople_SET() {
    Horse horse = new Horse( new Person( 42 ) { } );
    horse.setDriver( new Driver() { } );
}
@Test
public void test3_AHorseIsItsOwnEngine() {
    Horse  horse  = new Horse( new Person( 99 ) { } );
    Engine engine = horse.getEngine();
    assertTrue( "Incorrect result", horse == engine );
}
@Test(expected=IllegalArgumentException.class)
public void test4_HorsesCannotHaveAnEngineThatIsNotItself() {
    Engine anEngine = new Engine() {
        @Override
        public int compareTo(Engine o) {
            return 0;
        }
        @Override
        public int getForce() {
            return 0;
        }
    };
    Horse horse = new Horse( new Person( 24 ) { } );
    horse.setEngine( anEngine );
}
@Test
public void test5_AHorseReceivingNullOrItselfAsEngineSetEngineToItself() {
    Engine engine;
    Horse  horse  = new Horse( new Person( 99 ) { } );

    horse.setEngine( null );
    engine = horse.getEngine();
    assertTrue( "Incorrect result", horse == engine );

    horse.setEngine( horse );
    engine = horse.getEngine();
    assertTrue( "Incorrect result", horse == engine );
}
@Test
public void test6_HorseHasEquals() {
    Engine  a, b;
    boolean actual;
    // equal to itself
    a      = new Horse( new Person( 21 ));
    actual = a.equals( a );
    assertTrue ( "Incorrect result", actual );
    // equal to another horse (regardless of driver)
    a      = new Horse( new Person( 42 ));
    b      = new Horse( new Person( 24 ));
    actual = a.equals( b );
    assertTrue ( "Incorrect result", actual );
    // not equal to null
    actual = a.equals( null );
    assertFalse( "Incorrect result", actual );
    // not equal to some other object
    actual = a.equals( "84" );
    assertFalse( "Incorrect result", actual );
    // not equal to some other object
    actual = a.equals(  48  );
    assertFalse( "Incorrect result", actual );
}
@Test
public void test7_HorseHasCompareTo() {
    Horse a, b;
    int   actual;
    // equal to itself
    a      = new Horse( new Person( 52 ));
    actual = a.compareTo( a );
    assertTrue( "Incorrect result", actual == 0 );
    // equal to another horse (regardless of driver)
    a      = new Horse( new Person( 9 ));
    b      = new Horse( new Person( 8 ));
    actual = a.compareTo( b );
    assertTrue( "Incorrect result", actual == 0 );
}
}

1 个答案:

答案 0 :(得分:2)

test1中,你没有打电话来检查司机是否是一个人。你调用Horse构造函数,并使用调用super(driver,null)构造函数的Vehicle,但是你决不会调用setDriver方法,这是唯一的方法。检查driver instanceof Person。 (此外,driver只会被抛弃。Horse构造函数和Vehicle构造函数都不会将其保存在任何位置。Vehicle构造函数应该具有此功能:

this.driver = driver;
this.engine = engine;

我还没有尝试过您的代码,但test2失败的原因可能是setDriver实际抛出IllegalStateException,但您告诉JUnit要检查IllegalArgumentException }。 test4也是如此。首先解决这些问题,看看是否能解决问题。