如何声明一个接口类型的变量,然后将实现该接口的 Class 的对象分配给该变量,以及如何测试它?
How to declare a variable of type Interface and then assign to the variable an object of a Class that implements the Interface, and how to test this?
我正在为 classes 和它们实现的接口之间的交互而苦苦挣扎。具体来说,当class的构造函数需要参数时,如何声明一个Interface类型的变量,然后将一个实现该Interface的Class的对象赋值给该变量。我正在根据规范实现它,所以我不能只添加一个不需要参数的构造函数。
我有一个构造函数,它需要 2 个参数和 getter 个方法:
public class exampleImpl implements example{
private int var1;
private int var2;
public exampleImpl(int var1, int var2) {
if(var2< 0) {
var1 = 1;
var2 = 1;
}
public int getVar1(){
return Var1;
}
public int getVar2(){
return Var2;
}
然后我有一个方法"manipulate",它必须将接口作为输入,return将其作为输出:
public example manipulate(example e) {
example eInst = (exampleImpl)e;
int newVar1 = (this.getVar1()*eInst.getVar2() + this.getVar2()*eInst.getVar1());
int newVar2 = this.getVar2()*eInst.getVar2();
Pair<Integer, Integer> pair = normaliseExample(newVar1, newVar2);
int normalisedNewVar1 = pair.getKey();
int normalisedNewVar2 = pair.getValue();
example summedE = new exampleImpl(normalisedNewVar1, normalisedNewVar2);
return summedE;
}
接口本身仅将此方法定义为:
public interface example extends Example {
public example manipulate(example e);
}
我的方法声明一个接口example的变量,然后将一个实现example的exampleImpl对象赋值给该变量是否有效?它还必须 return 类型示例的变量,但我不知道发生这种情况时 newVar1 和 newVar2 去哪里,因为它们没有在接口中定义。
我还需要使用 JUnit 4 测试此方法,我完全不知道如何执行此操作,因为您无法实例化 example 类型的对象,因此我如何传递参数,例如:
class exampleImplTest {
@Test
public exampleImpl testManipulate()
{
assertEquals(example,exampleImpl.manipulate(example(3,4)));
}
这显示了一个错误,因为 manipulate 不是静态方法,但是当它需要一个对象 (this) 才能工作时,我如何才能使它成为一个静态方法?我可以在测试 class 中创建一个对象来调用这个测试吗?我想检查它 return 基于对象 "this" 和新对象 "e" 的正确操作。有办法吗?
如果这个问题引起了误解,我深表歉意,我是一个完全的初学者。我很难理解如何使用接口,因为规范要求我在这里使用它们。任何帮助将不胜感激。
(class和接口名称的开头我将使用大写字母)
Is my method valid in terms of declaring a variable of interface example and then assigning to the variable an object of exampleImpl that implements example?
是的,但在这种情况下不需要,您也不应该这样做。您假设通过参数 e
传入的对象将始终是一个 ExampleImpl
实例,因为您对该变量进行了硬编码转换。如果不是这样的 class,您将得到 ClassCastException
。在这种情况下,您可以删除
Example eInst = (ExampleImpl)e;
行并改用变量e
。
It must also return a variable of type example, but I do not know where newVar1 and newVar2 go when this happens since they are not defined in the interface.
您已经编写了代码
Example summedE = new ExampleImpl(normalisedNewVar1, normalisedNewVar2);
return summedE;
这将 return 一个实现 Example
接口的对象。所以这里一切都很好。如果您想使用 newVar1
和 newVar2
取决于您对 manipulate
方法的实现和要求。
Can I create an object within the test class to call this test on?
是的,这是通常的做法。写
Example obj = new ExampleImpl(4, 5);
在您的测试方法中创建一个您可以使用的 Example
对象。您可以使用不同的 Example
对象调用 manipulate()
方法,如下所示:
Example obj = new ExampleImpl(4, 5);
Example obj2 = new ExampleImpl(10, 20);
Example obj3 = obj.manipulate(obj2);
I want to check it returns the right manipulation based on an object "this" and the new object "e". Is there a way to do this?
这取决于在您的 Example
接口上定义了哪些其他方法。您已经写过它从另一个接口扩展而来,也命名为 Example
。根据 that 接口中定义的内容,您可以像这样在 Example
对象上调用其他方法:
Assert.assertEquals(42, obj3.getDifference()); // or whatever other methods you have
我正在为 classes 和它们实现的接口之间的交互而苦苦挣扎。具体来说,当class的构造函数需要参数时,如何声明一个Interface类型的变量,然后将一个实现该Interface的Class的对象赋值给该变量。我正在根据规范实现它,所以我不能只添加一个不需要参数的构造函数。
我有一个构造函数,它需要 2 个参数和 getter 个方法:
public class exampleImpl implements example{
private int var1;
private int var2;
public exampleImpl(int var1, int var2) {
if(var2< 0) {
var1 = 1;
var2 = 1;
}
public int getVar1(){
return Var1;
}
public int getVar2(){
return Var2;
}
然后我有一个方法"manipulate",它必须将接口作为输入,return将其作为输出:
public example manipulate(example e) {
example eInst = (exampleImpl)e;
int newVar1 = (this.getVar1()*eInst.getVar2() + this.getVar2()*eInst.getVar1());
int newVar2 = this.getVar2()*eInst.getVar2();
Pair<Integer, Integer> pair = normaliseExample(newVar1, newVar2);
int normalisedNewVar1 = pair.getKey();
int normalisedNewVar2 = pair.getValue();
example summedE = new exampleImpl(normalisedNewVar1, normalisedNewVar2);
return summedE;
}
接口本身仅将此方法定义为:
public interface example extends Example {
public example manipulate(example e);
}
我的方法声明一个接口example的变量,然后将一个实现example的exampleImpl对象赋值给该变量是否有效?它还必须 return 类型示例的变量,但我不知道发生这种情况时 newVar1 和 newVar2 去哪里,因为它们没有在接口中定义。
我还需要使用 JUnit 4 测试此方法,我完全不知道如何执行此操作,因为您无法实例化 example 类型的对象,因此我如何传递参数,例如:
class exampleImplTest {
@Test
public exampleImpl testManipulate()
{
assertEquals(example,exampleImpl.manipulate(example(3,4)));
}
这显示了一个错误,因为 manipulate 不是静态方法,但是当它需要一个对象 (this) 才能工作时,我如何才能使它成为一个静态方法?我可以在测试 class 中创建一个对象来调用这个测试吗?我想检查它 return 基于对象 "this" 和新对象 "e" 的正确操作。有办法吗?
如果这个问题引起了误解,我深表歉意,我是一个完全的初学者。我很难理解如何使用接口,因为规范要求我在这里使用它们。任何帮助将不胜感激。
(class和接口名称的开头我将使用大写字母)
Is my method valid in terms of declaring a variable of interface example and then assigning to the variable an object of exampleImpl that implements example?
是的,但在这种情况下不需要,您也不应该这样做。您假设通过参数 e
传入的对象将始终是一个 ExampleImpl
实例,因为您对该变量进行了硬编码转换。如果不是这样的 class,您将得到 ClassCastException
。在这种情况下,您可以删除
Example eInst = (ExampleImpl)e;
行并改用变量e
。
It must also return a variable of type example, but I do not know where newVar1 and newVar2 go when this happens since they are not defined in the interface.
您已经编写了代码
Example summedE = new ExampleImpl(normalisedNewVar1, normalisedNewVar2);
return summedE;
这将 return 一个实现 Example
接口的对象。所以这里一切都很好。如果您想使用 newVar1
和 newVar2
取决于您对 manipulate
方法的实现和要求。
Can I create an object within the test class to call this test on?
是的,这是通常的做法。写
Example obj = new ExampleImpl(4, 5);
在您的测试方法中创建一个您可以使用的 Example
对象。您可以使用不同的 Example
对象调用 manipulate()
方法,如下所示:
Example obj = new ExampleImpl(4, 5);
Example obj2 = new ExampleImpl(10, 20);
Example obj3 = obj.manipulate(obj2);
I want to check it returns the right manipulation based on an object "this" and the new object "e". Is there a way to do this?
这取决于在您的 Example
接口上定义了哪些其他方法。您已经写过它从另一个接口扩展而来,也命名为 Example
。根据 that 接口中定义的内容,您可以像这样在 Example
对象上调用其他方法:
Assert.assertEquals(42, obj3.getDifference()); // or whatever other methods you have