我有以下界面:
interface Parcel <VolumeType, WeightType> {
public VolumeType getVolume();
public WeightType getWeight();
}
我想定义一个类 A 来实现这个 Parcel,使得从这个类返回的体积和重量的类型为 Double,并且以下代码有效
Parcel<Double,Double> m = new A(1.0,2.0);
m.getVolume().toString()+m.getWeight().toString().equals("1.02.0");
我是泛型的新手,我对 A 定义的所有试验都失败了。有人可以告诉我一个关于如何定义这样一个类的例子吗?
我尝试了以下方法:
class A implements Parcel<Double, Double> {}
错误是
Constructor A in class A cannot be applied to given types;
Parcel<Double,Double> m = new A(1.0,2.0);
^
required: no arguments
found: double,double
reason: actual and formal argument lists differ in length
2 errors
答案 0 :(得分:1)
您添加了正确的 formatItems
子句。你得到的错误是你没有定义一个两参数的构造函数:
implements
您还需要实现两个接口方法:
class A implements Parcel<Double, Double> {
public A(double volume, double weight) {
...
}