Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java泛型可分配性_Java_Generics_Type Parameter_Generic Collections - Fatal编程技术网

Java泛型可分配性

Java泛型可分配性,java,generics,type-parameter,generic-collections,Java,Generics,Type Parameter,Generic Collections,我在泛型类型参数的赋值能力上有点困难 以下是我的类型定义的外观: public static interface CellValue<T> { T getValue(); Class<?> getType(); } public static class DoubleCell implements CellValue<Double> { Double value; public DoubleCell(Double value

我在泛型类型参数的赋值能力上有点困难

以下是我的类型定义的外观:

public static interface CellValue<T> {
    T getValue();
    Class<?> getType();
}

public static class DoubleCell implements CellValue<Double> {
    Double value;

    public DoubleCell(Double value) {
        super();
        this.value = value;
    }

    @Override
    public Double getValue() {
        return value;
    }

    @Override
    public Class<?> getType() {
        return Double.class;
    }
}

public static class FormulaCell implements CellValue<String> {
    String value;

    public FormulaCell(String value) {
        this.value = value;
    }

    @Override
    public String getValue() {
        return value;
    }

    @Override
    public Class<?> getType() {
        return String.class;
    }
}
现在,为什么下面的声明对我有效,这是预期的行为,但我可能误解了它为什么有效

Map<String, ? extends CellValue<?>> m = new HashMap<String, FormulaCell>();
Map<String, ? extends CellValue<?>> m2 = new HashMap<String, DoubleCell>();
…而下列情况则不存在

Map<Integer, Map<String, ? extends CellValue<?>>> n = new HashMap<Integer, Map<String, FormulaCell>>();
Map<Integer, Map<String, ? extends CellValue<?>>> n2 = new HashMap<Integer, Map<String, DoubleCell>>();

试试地图试试地图会有帮助吗?谢谢!这很有帮助。嵌套通配符是我会尽量避免使用的。会有帮助吗?谢谢!这很有帮助。嵌套通配符是我将尽量避免使用的。