Java 使用泛型类型实现接口

Java 使用泛型类型实现接口,java,generics,Java,Generics,我有以下界面: interface Parcel <VolumeType, WeightType> { public VolumeType getVolume(); public WeightType getWeight(); } 接口包{ public VolumeType getVolume(); 公共权重类型getWeight(); } 我想定义一个实现这个包裹的类a,这样从这个类返回的体积和重量的类型是Double,下面的代码是 Parcel<Dou

我有以下界面:

interface Parcel <VolumeType, WeightType> {
    public VolumeType getVolume();
    public WeightType getWeight();
}
接口包{
public VolumeType getVolume();
公共权重类型getWeight();
}
我想定义一个实现这个包裹的类a,这样从这个类返回的体积和重量的类型是Double,下面的代码是

Parcel<Double,Double> m = new A(1.0,2.0);
m.getVolume().toString()+m.getWeight().toString().equals("1.02.0");
地块m=新A(1.0,2.0);
m、 getVolume().toString()+m.getWeight().toString()等于(“1.02.0”);
我对泛型药和我所有关于A定义的试验都是新手。有人能给我举个例子说明如何定义这样一个类吗

我尝试了以下方法:

class A implements Parcel<Double, Double> {}
A类实现包裹{}
错误是

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
类A中的构造函数A不能应用于给定类型; 地块m=新A(1.0,2.0); ^ 必需:无参数 找到:双人,双人 原因:实际参数列表和正式参数列表长度不同 2个错误
您添加了正确的
implements
子句。您得到的错误是您没有定义双参数构造函数:

class A implements Parcel<Double, Double> {
    public A(double volume, double weight) {
        ...
    }

您的类将需要两个字段、一个设置它们的构造函数以及接口中声明的两个getter方法。向我们展示您的最佳尝试,我们将纠正任何错误。
    public Double getVolume() {
        ...
    }

    public Double getWeight() {
        ...
    }
}