泛型-“捕获<!-?->”不能应用于“?”

时间:2018-08-26 12:56:24

标签: java generics

在泛型错误消息中“ caputure<?>”是什么意思?考虑以下示例:

interface Model<T> { T get(); }

static class DefaultModel<T> implements Model<T> {
    final T t;
    public DefaultModel(T t) { this.t = t; }
    @Override public T get() { return t; }
}

interface CellPopulator<T> {}

interface Column<T,S> extends CellPopulator<T> {}

static class Row {}

public static class Table<T,S> {
    public void updateCellItem(Model<Column<T,S>> model) {}
}

使用未解决冲突的类型用法:

public static void main(String[] args) {
    Model<CellPopulator<Row>> gridCellModel = null;

    Table<Row,?> table = new Table<>();
    Column<Row,?> tableCell = (Column<Row, ?>) gridCellModel.get();
    Model<Column<Row,?>> tableCellModel = new DefaultModel<>(tableCell);

    // Model<Column<Row,capture<?>>> can not be applied to Model<Column<Row,?>>
    table.updateCellItem(tableCellModel); // COMPILATION ERROR
}

0 个答案:

没有答案