gpt4 book ai didi

java - 泛型对象创建在 Eclipse 中显示错误,但在 Maven 中编译

转载 作者:行者123 更新时间:2023-11-30 03:30:36 25 4
gpt4 key购买 nike

嗨,我有一些初始化如下......它在 Eclipse 中显示错误,因为类型不匹配。如果我通过命令提示符使用 maven 进行编译,它会编译没有任何错误..使用 eclipse luna,并将编译器设置为 java 1.7。

  ReportData<Object> rdata = null;

rdata = new ReportData<>(reportCtx.get()); // error shown for this line in //eclipse.

ReportData<T>{

private T val;
public ReportData(T val){
this.val=val;
}
}

我不明白,为什么 Eclipse 显示错误,以及它如何使用相同的 java 1.7 在 Maven 中编译。 rdata = new ReportData<>(reportCtx.get()); 有什么问题吗?初始化。

最佳答案

ReportData<?>是各类报表数据的父类(super class)型。

ReportData<Object>不是各种报表数据的父类(super class)型。

因此您不能分配:

ReportData<Object>ReportData<of an unknown kind>

参见The Java™ Tutorials , Generics, Wildcards .

顺便说一句,除了......

ReportData<Object> rdata = new ReportData<Object>(reportCtx.get());

...中的不同声明...

ReportData<?> rdata = new ReportData<>(reportCtx.get());

...也能达到目的。

关于java - 泛型对象创建在 Eclipse 中显示错误,但在 Maven 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29146046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com