1

我制作了一个引擎类,如下所示。基于

Engine:扩展 Java 的 Comparable(用于在引擎之间进行比较)并声明整数 getter 方法“getForce”的接口,这表明引擎子类将具有它们能够
产生的力。

public interface Engine extends Comparable<Engine>{

public int getForce();

}

我正在尝试AbstractEngine根据以下描述创建一个类。

AbstractEngine: 大多数引擎实现的抽象类。它是 Engine 的子类,并有一个整数字段指示其力。该字段通过构造函数进行初始化。该类覆盖getForcefromEnginecompareTofrom Comparable。两个引擎的比较是通过找出它们的力之间的差异来完成的(以这样一种方式对一组引擎进行排序)。我对覆盖方法Engine并确保与AbstractEnginehas 进行比较感到困惑Engine

这是我目前所拥有的,但它没有通过 JUnit 测试检查是否AbstractEnginegetForce,equalsCompareTo. 有没有我必须扩展方法的特定方法?

abstract class AbstractEngine implements Engine {

public AbstractEngine(int force){

}
public int compareTo(Engine o) {
    // TODO Auto-generated method stub
    return 0;
}
}

这是junit测试

import static org.junit.Assert.*;

import org.junit.Test;

public class EngineTest {

@Test
public void test0_EngineImplementsComparableAndDefinesGetForce() {
    Engine engine = new Engine() {
        @Override
        public int compareTo(Engine o) {
            return 0;
        }
        @Override
        public int getForce() {
            return 0;
        }
    };
    assertTrue( "Incorrect result", engine instanceof Comparable );
}
@Test
public void test1_AbstractEngineIsAnEngine() {
    Engine engine = new AbstractEngine( 2 ) { };
    assertTrue( "Incorrect result", engine instanceof Engine );
}
@Test
public void test2_AbstractEngineHasGetForce() {
    Engine engine   = new AbstractEngine( 24 ) { };
    int    actual   = engine.getForce();
    int    expected = 24;
    assertEquals( "Incorrect result", expected, actual );
}
@Test
public void test3_AbstractEngineHasEquals() {
    Engine  a, b;
    boolean actual;
    // equal to itself
    a      = new AbstractEngine( 42 ) { };
    actual = a.equals( a );
    assertTrue ( "Incorrect result", actual );
    // equal to another engine with the same force
    a      = new AbstractEngine( 19 ) { };
    b      = new AbstractEngine( 19 ) { };
    actual = a.equals( b );
    assertTrue ( "Incorrect result", actual );
    // not equal to another engine with a different force 
    a      = new AbstractEngine( 22 ) { };
    b      = new AbstractEngine( 24 ) { };
    actual = a.equals( b );
    assertFalse( "Incorrect result", actual );
    // not equal to null
    actual = a.equals( null );
    assertFalse( "Incorrect result", actual );
    // not equal to some other object
    actual = a.equals( "22" );
    assertFalse( "Incorrect result", actual );
    // not equal to some other object
    actual = a.equals(  22  );
    assertFalse( "Incorrect result", actual );
}
@Test
public void test3_AbstractEngineHasCompareTo() {
    Engine a, b;
    int    actual;
    // equal to itself
    a      = new AbstractEngine( 42 ) { };
    actual = a.compareTo( a );
    assertTrue( "Incorrect result", actual == 0 );
    // equal to another engine with the same force
    a      = new AbstractEngine( 9000 ) { };
    b      = new AbstractEngine( 9000 ) { };
    actual = a.compareTo( b );
    assertTrue( "Incorrect result", actual == 0 );
    // goes before a more powerful engine 
    a      = new AbstractEngine( 23 ) { };
    b      = new AbstractEngine( 24 ) { };
    actual = a.compareTo( b );
    assertTrue( "Incorrect result", actual < 0 );
    // goes after a less powerful engine 
    actual = b.compareTo( a );
    assertTrue( "Incorrect result", actual > 0 );
}
@Test
public void test4_OxIsAnEngine() {
    Ox ox = new Ox( 3 );
    assertTrue( "Incorrect result", ox instanceof AbstractEngine );
}
@Test
public void test5_OxHasGetForce() {
    Engine engine   = new Ox( 4 );
    int    actual   = engine.getForce();
    int    expected = 4;
    assertEquals( "Incorrect result", expected, actual );
}
@Test
public void test5_OxHasEquals() {
    Engine  a, b;
    boolean actual;
    // equal to itself
    a      = new Ox( 42 );
    actual = a.equals( a );
    assertTrue ( "Incorrect result", actual );
    // equal to another engine with the same force
    a      = new Ox( 19 );
    b      = new Ox( 19 );
    actual = a.equals( b );
    assertTrue ( "Incorrect result", actual );
    // not equal to another engine with a different force 
    a      = new Ox( 22 );
    b      = new Ox( 24 );
    actual = a.equals( b );
    assertFalse( "Incorrect result", actual );
    // not equal to another engine of equal force 
    a      = new Ox            ( 21 );
    b      = new AbstractEngine( 21 ) { };
    actual = a.equals( b );
    assertFalse( "Incorrect result", actual );
    // not equal to null
    actual = a.equals( null );
    assertFalse( "Incorrect result", actual );
    // not equal to some other object
    actual = a.equals( "blah" );
    assertFalse( "Incorrect result", actual );
    // not equal to some other object
    actual = a.equals(  111  );
    assertFalse( "Incorrect result", actual );
}
}
4

1 回答 1

1
  • 在 的构造函数中AbstractEngine,需要设置force成员变量。
  • 如果此方法看起来与类中的完全相同,则不必在其中包含getForce()方法。AbstractClassEngine
  • 您必须在类或类compareTo中实现分配中描述的方法。EngineAbstractEngine

阅读描述,force成员变量不应该在AbstractEngine类而不是Engine类中吗?(如果你把它放在AbstractEngine,那么getForce()也应该放在AbstractEngine而不是Engine)。

于 2013-10-01T20:15:15.843 回答