简体   繁体   English

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

[英]generics object creation showing error in eclipse, but compiles in maven

Hi I have some initialization as below.... it is showing error in eclipse as type mismatch. 嗨,我有一些初始化如下...。它显示eclipse中的错误,类型不匹配。 if i compile using maven through command prompt, it compiles without any error.. using eclipse luna, and compiler set to java 1.7. 如果我通过命令提示符使用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;
      }
  }

I am not understanding, why eclipse show error for this and how it compiles in maven with the same java 1.7. 我不明白,为什么eclipse为此显示错误,以及它如何在maven中使用相同的Java 1.7进行编译。 whats wrong with rdata = new ReportData<>(reportCtx.get()); rdata = new ReportData<>(reportCtx.get()); initialization. 初始化。

ReportData<?> is the supertype of all kinds of report data. ReportData<?>是各种报告数据的超类型。

ReportData<Object> is not the supertype of all kinds of report data. ReportData<Object> 不是所有报表数据的超类型。

Hence you cannot assign: 因此,您不能分配:

ReportData<Object>ReportData<of an unknown kind> ReportData<Object>ReportData<of an unknown kind>

See The Java™ Tutorials , Generics, Wildcards . 请参阅Java™教程,泛型,通配符

And BTW, in addition to ... 和顺便说一句,除了...

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

... the different declaration in ... ......中的不同声明

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

... does the trick, as well. ……也能解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM