我有一个如下所示的类 Horse 我必须使 Horse 对象不采用不是 Perso 对象的驱动程序,并且它继承了 Vehicle 类并且不能具有不是自身的引擎。在我的代码下发布的 Junit 测试中,我的 test4、test1 和 test2 失败了。我相信问题在于我如何专门处理设置引擎和设置驱动程序方法。在这种情况下,我需要设置方法吗?
马.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;
}
}
车辆.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;
}
}
引擎.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 );
}
}