Java泛型代码在eclipse中编译,但不在命令行中编译 [英] Java generics code compiles in eclipse but not in command line

查看:109
本文介绍了Java泛型代码在eclipse中编译,但不在命令行中编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道过去有几个问题在eclipse中编译但不在命令行中,但是我找不到我的问题的答案。



特别是,我认为我可以设置eclipse来使用我的系统编译器,但是这个问题依然没有解决。



我目前正在检查: '首选项 - > Java - >安装的JRE'。



这只包含一个JRE是我的系统。



以下是问题



我有一个java泛型类,作为参数的枚举类型,如下所示:

  public class MyClass< T extends Enum< T>> 

在类内部我将一个已知的枚举值与T的值进行比较。例如,假设我有这个枚举:

  public enum OtherEnum {
a,
b
}

然后我测试:

  protected void foo(T enumVal){
if(enumVal == OtherEnum.a){
// do something
}
else if(enumVal == OtherEnum .b){
// do something else
}
}

这个编译没有问题在eclipse,但在命令行javac我得到这个错误:


无与伦比的类型:T和OtherEnum


我在使用java 1.6(1.6.0_26和1.6.0_16)变体的两个系统上尝试过。一个是Mac,另一个是linux。他们都提供相同的错误,而eclipse正在编译,没有任何问题。



所以:


  1. 如何确定哪个编译器eclipse正在使用?


  2. 这里还有什么问题?

谢谢!

解决方案

单独的编译器...使用==强制执行更严格的编译时间检查,因此在CL中执行类型比较和错误。在Eclipse中,您可以管理编译时间的设置,并放宽限制。



您可以尝试Enum.equals(),并且可能不会执行这样严格的编译时间类型比较。只是一个想法。


I know there have been several questions in the past regarding things that compile in eclipse but not in command line, but I could not find an answer to my problem yet.

In particular, I think that I was able to set eclipse to use my system compiler, but that still did not solve the problem.

I am currently checking under : 'Preferences -> Java -> Installed JREs'.

This contains only one JRE which is my system one.

Here are the specifics of the problem

I have a java generic class that takes as argument an Enum type, like so:

public class MyClass<T extends Enum<T>>

Somewhere inside the class I compare a known enum value with values of T. So for example let's assume I have this enum:

public enum OtherEnum{
 a,
 b
}

And then I test:

protected void foo(T enumVal){
    if(enumVal == OtherEnum.a){
        // do something
    }
    else if(enumVal == OtherEnum.b){
        // do something else
    }
}

This compiles with no problem in eclipse but in command line javac I get this error:

incomparable types: T and OtherEnum

I tried this on two systems that use variant of java 1.6 (1.6.0_26 and 1.6.0_16). One is a Mac, the other a linux. They both give the same error, while eclipse is compiling away with no problem.

So:

  1. How do I make sure which compiler eclipse is using?

  2. What is even the problem here?

Thanks!

解决方案

Separate compilers...The use of == forces a stricter compile time check, therefore in the CL it is enforcing type comparison and erroring. In Eclipse you can manage the settings for the compile time and loosen the restrictions so to speak.

You can try Enum.equals() and it may not enforce such strict compile time type comparison. just a thought.

这篇关于Java泛型代码在eclipse中编译,但不在命令行中编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆