如何在java中扩展抽象类

时间:2013-10-01 20:09:59

标签: java inheritance polymorphism abstract-class abstract

我有一个引擎类,如下所示。基于

引擎:扩展Java的Comparable(在引擎之间进行比较)并声明一个整数的接口 getter方法“getForce”,表示引擎子类将具有它们能够具有的力 制备

public interface Engine extends Comparable<Engine>{

public int getForce();

}

我正在尝试基于以下描述创建AbstractEngine类。

AbstractEngine:大多数引擎实现的抽象类。它是Engine的子类,有一个整数字段表示它的力。该字段通过构造函数初始化。该类会覆盖getForce中的EnginecompareTo中的Comparable。两个引擎的比较是通过找到它们的力之间的差异来完成的(以这种方式排序引擎集合排列。我对于覆盖Engine中的方法并确保AbstractEngine具有与Engine有比较。

这就是我目前所拥有的,但是如果AbstractEngine具有getForceequalsCompareTo,则JUnit测试检查失败。我是否有必要扩展方法?

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

1 个答案:

答案 0 :(得分:1)

  • AbstractEngine的构造函数中,您需要设置force成员变量。
  • 如果此方法与类getForce()中的方法完全相同,则AbstractClass中没有Engine方法。
  • 您必须在compareTo课程或Engine课程中实施作业中所述的AbstractEngine方法。

阅读说明,force成员变量不应该在AbstractEngine类而不是Engine类中吗? (如果您将其放在AbstractEngine中,那么getForce()也应该在AbstractEngine而不是Engine。)