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

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

问题描述

我知道过去有几个关于在 eclipse 中编译但在命令行中没有的问题,但我还没有找到我的问题的答案.

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.

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

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

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

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

这仅包含一个 JRE,这是我的系统之一.

This contains only one JRE which is my system one.

这里是问题的细节

我有一个 Java 泛型类,它将 Enum 类型作为参数,如下所示:

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

public class MyClass<T extends Enum<T>>

在类中的某个地方,我将一个已知的枚举值与 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
}

然后我测试:

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

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

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

无与伦比的类型:T 和 OtherEnum

incomparable types: T and OtherEnum

我在两个使用 java 1.6 变体(1.6.0_26 和 1.6.0_16)的系统上尝试了这个.一个是Mac,另一个是Linux.他们都给出了同样的错误,而 eclipse 编译没有问题.

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.

所以:

  1. 如何确定 eclipse 使用的是哪个编译器?

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

这里到底有什么问题?

谢谢!

推荐答案

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

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.

您可以尝试 Enum.equals() 并且它可能不会强制执行如此严格的编译时类型比较.只是一个想法.

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

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

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