【问题标题】:Horse object inheritance马对象继承
【发布时间】:2013-10-02 19:13:04
【问题描述】:

我有一个如下所示的 Horse 类,我必须确保 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 测试

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 );
}
}

【问题讨论】:

  • 这段代码设计得很糟糕。在这种情况下,继承没有意义。重新设计继承层次结构。
  • 为什么马需要装发动机,而车不需要?

标签: java inheritance methods junit


【解决方案1】:

test1 中,您不会调用任何会检查驱动程序是否是人的东西。你调用Horse 构造函数,它使用super(driver,null) 调用Vehicle 构造函数,但你绝不会调用setDriver 方法,这是唯一会检查driver instanceof Person 的方法。 (此外,driver 刚刚被丢弃。Horse 构造函数和 Vehicle 构造函数都不会将其保存在任何地方。Vehicle 构造函数可能应该有这个:

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

)

我没有尝试过你的代码,但test2 失败的原因可能是setDriver 实际上抛出了IllegalStateException,但你告诉JUnit 检查IllegalArgumentExceptiontest4 也一样。先解决这些问题,看看能不能解决问题。

【讨论】:

  • 我在测试 1 中也写了同样的内容。测试 2 和 4 中异常的类型不匹配很有意义。只需 +1。
  • ok 设法解决了除 test1 之外的所有问题。我有点理解你的意思,但对如何更改我的代码来实现它感到困惑
  • @Darksole 简单的解决方案是让Horse 构造函数执行相同的检查,如果失败则抛出异常。 (会有一些重复的代码,我会将“驱动程序检查”放在一个方法中,以便它只在一个地方。但现在,先让它工作。)
猜你喜欢
  • 2012-01-05
  • 2011-10-08
  • 2013-01-04
  • 2019-01-30
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多