如何在java中扩展抽象类

如何在java中扩展抽象类,java,inheritance,polymorphism,abstract-class,abstract,Java,Inheritance,Polymorphism,Abstract Class,Abstract,我制作了一个引擎类,如下所示。基于 引擎:扩展Java的Compariable(在引擎之间进行比较)并声明整数的接口 getter方法“getForce”,它表示引擎子类将具有它们能够承受的力 生产 public interface Engine extends Comparable<Engine>{ public int getForce(); } 这是junit测试 import static org.junit.Assert.*; import org.junit.Te

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

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

public interface Engine extends Comparable<Engine>{

public int getForce();

}
这是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 );
}
}
import static org.junit.Assert.*;
导入org.junit.Test;
公共级引擎测试{
@试验
public void test0_engineimplements比较leanddefinesgetforce(){
发动机=新发动机(){
@凌驾
公共内部比较(发动机o){
返回0;
}
@凌驾
公共int getForce(){
返回0;
}
};
assertTrue(“结果不正确”,发动机实例可比);
}
@试验
公共无效测试1_AbstractEngineesAnengine(){
引擎引擎=新的抽象引擎(2){};
assertTrue(“结果不正确”,发动机的发动机实例);
}
@试验
public void test2_abstractEngineeHasgetForce(){
引擎引擎=新的抽象引擎(24){};
int实际值=engine.getForce();
int预期为24;
assertEquals(“结果不正确”,预期、实际);
}
@试验
公共无效测试3_AbstractEngineeHaseQuals(){
发动机a、b;
布尔实数;
//不相上下
a=新的抽象引擎(42){};
实际=a。等于(a);
资产真实(“结果不正确”,实际);
//相当于另一台具有相同作用力的发动机
a=新的抽象引擎(19){};
b=新的抽象引擎(19){};
实际=a.等于(b);
资产真实(“结果不正确”,实际);
//不等于另一台具有不同作用力的发动机
a=新的抽象引擎(22){};
b=新的抽象引擎(24){};
实际=a.等于(b);
资产虚假(“结果不正确”,实际);
//不等于null
实际值=a.等于(空);
资产虚假(“结果不正确”,实际);
//不等于其他物体
实际值=a等于(“22”);
资产虚假(“结果不正确”,实际);
//不等于其他物体
实际=a.等于(22);
资产虚假(“结果不正确”,实际);
}
@试验
public void test3_abstractEngineehascompareto(){
发动机a、b;
int实际值;
//不相上下
a=新的抽象引擎(42){};
实际值=a。与(a)相比;
assertTrue(“结果不正确”,实际==0);
//相当于另一台具有相同作用力的发动机
a=新的抽象引擎(9000){};
b=新的抽象引擎(9000){};
实际=a.与(b)相比;
assertTrue(“结果不正确”,实际==0);
//在一个更强大的引擎之前
a=新的抽象引擎(23){};
b=新的抽象引擎(24){};
实际=a.与(b)相比;
assertTrue(“结果不正确”,实际值<0);
//去追求一个动力较弱的引擎
实际=b.与(a)相比;
assertTrue(“结果不正确”,实际值>0);
}
@试验
公共无效测试4_OxIsAnEngine(){
牛=新牛(3);
assertTrue(“结果不正确”,抽象引擎的ox实例);
}
@试验
公共无效测试5_OxHasGetForce(){
发动机=新的Ox(4);
int实际值=engine.getForce();
int预期=4;
assertEquals(“结果不正确”,预期、实际);
}
@试验
公共无效测试5_oxhaseequals(){
发动机a、b;
布尔实数;
//不相上下
a=新牛(42);
实际=a。等于(a);
资产真实(“结果不正确”,实际);
//相当于另一台具有相同作用力的发动机
a=新牛(19);
b=新牛(19);
实际=a.等于(b);
资产真实(“结果不正确”,实际);
//不等于另一台具有不同作用力的发动机
a=新牛(22);
b=新牛(24);
实际=a.等于(b);
资产虚假(“结果不正确”,实际);
//不等于另一个同等力量的发动机
a=新牛(21);
b=新的抽象引擎(21){};
实际=a.等于(b);
资产虚假(“结果不正确”,实际);
//不等于null
实际值=a.等于(空);
资产虚假(“结果不正确”,实际);
//不等于其他物体
实际=等于(“废话”);
资产虚假(“结果不正确”,实际);
//不等于其他物体
实际值=a等于(111);
资产虚假(“结果不正确”,实际);
}
}
  • AbstractEngine
    的构造函数中,需要设置
    force
    成员变量
  • 如果此方法与类
    引擎中的方法完全相同,则无需在
    抽象类中使用
    getForce()
    方法
  • 您必须在
    Engine
    类或
    AbstractEngine
    类中实现作业中描述的
    compareTo
    方法
阅读说明时,
force
成员变量不应该在
AbstractEngine
类中,而应该在
Engine
类中吗?(如果你把它放在
AbstractEngine
中,那么
getForce()
也应该放在
AbstractEngine
中,而不是
Engine
)。

  • AbstractEngine
    的构造函数中,需要设置
    force
    成员变量
  • 如果此方法与类
    引擎中的方法完全相同,则无需在
    抽象类中使用
    getForce()
    方法
  • 您必须在
    Engine
    类或
    AbstractEngine
    类中实现作业中描述的
    compareTo
    方法
  • 阅读说明时,
    force
    成员变量不应该在
    AbstractEngine
    类中,而应该在
    Engine
    类中吗?(如果将其放在
    抽象引擎中
    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 );
    }
    }