我制作了一个引擎类,如下所示。基于
Engine:扩展Java的Comparable(在引擎之间进行比较)并声明一个整数的接口(interface)
getter 方法“getForce”,表示引擎子类将拥有它们能够承受的力量
生产。
public interface Engine extends Comparable<Engine>{
public int getForce();
}
我正在尝试根据以下描述创建一个 AbstractEngine
类。
AbstractEngine
:大多数引擎实现的抽象类。它是 Engine 的子类,并有一个指示其力量的整数字段。该字段通过构造函数初始化。该类重写了 Engine
中的 getForce
和 Comparable
中的 compareTo
。两个引擎的比较是通过查找它们的力量之间的差异来完成的(以对引擎集合进行排序的方式进行排列)。我对覆盖 Engine
中的方法并确保 AbstractEngine
与 Engine
进行比较感到困惑。
这就是我目前所拥有的,但它无法通过 JUnit 测试检查 AbstractEngine
是否具有 getForce
、equals
和 CompareTo
。我是否有特定的方法来扩展这些方法?
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 );
}
}
最佳答案
- 在
AbstractEngine
的构造函数中,需要设置force
成员变量。 - 如果
AbstractClass
中的getForce()
方法看起来与类Engine
中的完全相同,则无需在该方法中使用getForce()
方法。 - 您必须按照作业中的描述,在
Engine
类或AbstractEngine
类中实现compareTo
方法。
读了描述,force
成员变量不应该在AbstractEngine
类中而不是在Engine
类中吗? (如果将其放入 AbstractEngine
中,则 getForce()
也应该位于 AbstractEngine
中,而不是 Engine
中)。
关于java - java中如何扩展抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19124984/