如何在java中扩展抽象类 [英] how to extend a abstract class in java
问题描述
我制作了一个引擎类,如下所示.基于
I have a made an engine class that is as follows. based on
Engine:扩展 Java 的 Comparable(在引擎之间进行比较)并声明一个整数的接口getter 方法getForce",它表示引擎子类将拥有它们能够施加的力
生产.
Engine: Interface that extends Java’s Comparable (to compare among engines) and declares an integer
getter method "getForce", which indicatesthat engine subclasses will have a force they are capable of
producing.
public interface Engine extends Comparable<Engine>{
public int getForce();
}
我正在尝试根据以下描述创建一个 AbstractEngine
类.
I am trying to make an AbstractEngine
class based on the following description.
AbstractEngine
:大多数引擎实现的抽象类.它是 Engine 的子类,并有一个表示其力的整数字段.该字段通过构造函数初始化.该类覆盖了 Engine
中的 getForce
和 Comparable
中的 compareTo
.两个引擎的比较是通过找出它们的力之间的差异来完成的(以排序引擎集合的方式排列.我对覆盖 Engine
中的方法并确保 AbstractEngine
与 Engine
有比较.
AbstractEngine
: Abstract class implemented by most engines. It subclasses from Engine and has an integer field indicating its force. This field is initialized through a constructor. The class overrides getForce
from Engine
and compareTo
from Comparable
. The comparison of two engines is done by finding the difference between their forces (in such a way that sorting a collection of engines arranges. I am confused on overriding the methods from Engine
and making sure AbstractEngine
has the compare to the Engine
has.
这是我目前所拥有的,但它在 JUnit 测试中失败了,检查 AbstractEngine
是否具有 getForce
、equals
和 <代码>比较代码>.是否有特定的方法来扩展这些方法?
This is what I have at the moment, but it's failing a JUnit test checking if AbstractEngine
has the getForce
, equals
, and CompareTo
. Is there a specific way I have to extend the methods?
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
类中的方法完全相同.- 您必须在
Engine
类或AbstractEngine
类中实现分配中描述的compareTo
方法.莉>- In the constructor of
AbstractEngine
, you need to set theforce
member variable. - It is not necessary to have a
getForce()
method inAbstractClass
, if this method looks exactly the same as in classEngine
. - You have to implement the
compareTo
method as described in the assignment, either in theEngine
class or in theAbstractEngine
class.
阅读说明,
force
成员变量不应该在AbstractEngine
类中而不是Engine
类中吗?(如果你把它放在AbstractEngine
中,那么getForce()
也应该在AbstractEngine
而不是Engine
中).Reading the description, shouldn't the
force
member variable be in theAbstractEngine
class instead of theEngine
class? (If you put it inAbstractEngine
, thengetForce()
should also be inAbstractEngine
instead ofEngine
).这篇关于如何在java中扩展抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- In the constructor of