简体   繁体   English

如何在Java中扩展抽象类

[英]how to extend a abstract class in java

I have a made an engine class that is as follows. 我有一个引擎类,如下所示。 based on 基于

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 引擎:扩展Java的Comparable(在引擎之间进行比较)并声明整数getter方法“ getForce”的接口,该方法指示引擎子类将具有它们能够执行的作用力
producing. 生产。

public interface Engine extends Comparable<Engine>{

public int getForce();

}

I am trying to make an AbstractEngine class based on the following description. 我正在尝试根据以下描述创建一个AbstractEngine类。

AbstractEngine : Abstract class implemented by most engines. AbstractEngine :大多数引擎实现的抽象类。 It subclasses from Engine and has an integer field indicating its force. 它是Engine的子类,并具有指示其作用力的整数字段。 This field is initialized through a constructor. 该字段通过构造函数初始化。 The class overrides getForce from Engine and compareTo from Comparable . 类覆盖getForceEnginecompareToComparable 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. 比较两个引擎是通过找到它们之间的力差来进行的(以排序引擎集合的方式进行排列。我对重写Engine的方法感到困惑,并确保AbstractEngine具有与Engine比较的能力。

This is what I have at the moment, but it's failing a JUnit test checking if AbstractEngine has the getForce , equals , and CompareTo . 这是我目前所拥有的,但是它无法通过JUnit测试来检查AbstractEngine是否具有getForceequalsCompareTo 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;
}
}

here is the junit test 这是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 );
}
}
  • In the constructor of AbstractEngine , you need to set the force member variable. AbstractEngine的构造函数中,您需要设置force成员变量。
  • It is not necessary to have a getForce() method in AbstractClass , if this method looks exactly the same as in class Engine . 如果此方法看起来与类Engine方法完全相同,则getForce()AbstractClass具有getForce()方法。
  • You have to implement the compareTo method as described in the assignment, either in the Engine class or in the AbstractEngine class. 您必须在Engine类或AbstractEngine类中实现分配中所述的compareTo方法。

Reading the description, shouldn't the force member variable be in the AbstractEngine class instead of the Engine class? 阅读说明, force成员变量不应该在AbstractEngine类而不是Engine类中吗? (If you put it in AbstractEngine , then getForce() should also be in AbstractEngine instead of Engine ). (如果将其放在AbstractEngine ,则getForce()也应该位于AbstractEngine而不是Engine )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM