Java:在Eclipse和javac中处理异常的泛型 [英] Java: Generics handled differenlty in Eclipse and javac
问题描述
说我们有类
public class Entry< K,V> {
public Entry(K k,V v){
mKey = k;
mValue = v;
}
public K getKey(){
return mKey;
}
public V getValue(){
return mValue;
}
...
}
然后我们写一个扩展类,如:
public class ExtendedEntry< K extends SomeSpecificKey,V>条目< K,V>
然后,层次结构中还有一个类:
public class ConcreteEntry< Option extends Something,K extends MoreSpecificKey< Option>,V>扩展ExtendedEntry< K,V>
MoreSpecificKey
here:
public interface MoreSpecificKey< OptionType extends Something>扩展SomeSpecificKey {
public void doSomething(OptionType option);
}
我使用 doSomething()
方法在 ConcreteEntry
的代码中。
然后,Eclipse编译器说OK并构建代码并显示no警告。
但是 javac
回复:
... ConcreteEntry.java:6类型... ExtendedEntry和... ExtendedEntry不兼容:都定义getKey()方法,但不相关的返回类型
当我更改(删除通用类型) MoreSpecificKey
到:
public interface MoreSpecificKey extends SomeSpecificKey {
public< OptionType extends Something> void doSomething(OptionType选项);
}
ConcreteEntry
:
public class ConcreteEntry< Option extends Something,K extends MoreSpecificKey,V>扩展ExtendedEntry< K,V>
javac
中的错误已经消失,编译好也可以。
那么,Eclipse如何编译这样的代码是什么 - 它是编译器参数定义的还是不同的首选项,或编译器的差异。如何在Eclipse中处理(修复)这一点,并确保它将在 javac
中编译。
部分内容已在问题 Eclipse中找到javac来编译项目的位置? a> - Eclipse使用不同的编译器
Say we have class
public class Entry<K, V> {
public Entry(K k,V v) {
mKey = k;
mValue = v;
}
public K getKey() {
return mKey;
}
public V getValue() {
return mValue;
}
...
}
Then we write an extending class like:
public class ExtendedEntry<K extends SomeSpecificKey, V> extends Entry<K, V>
Then, one class more in the hierarchy:
public class ConcreteEntry<Option extends Something, K extends MoreSpecificKey<Option>, V> extends ExtendedEntry<K, V>
MoreSpecificKey
here:
public interface MoreSpecificKey<OptionType extends Something> extends SomeSpecificKey {
public void doSomething(OptionType option);
}
And I use doSomething()
method somewhere in the code of ConcreteEntry
.
Then Eclipse compiler says OK and builds code and shows no warnings.
But javac
responses:
...ConcreteEntry.java:6 types ...ExtendedEntry and ...ExtendedEntry are incompatible: both define getKey() method, but unrelated return types
When I change (removing generic type) MoreSpecificKey
to:
public interface MoreSpecificKey extends SomeSpecificKey {
public <OptionType extends Something> void doSomething(OptionType option);
}
ConcreteEntry
definition now:
public class ConcreteEntry<Option extends Something, K extends MoreSpecificKey, V> extends ExtendedEntry<K, V>
Error in javac
is gone, and everything is compiled OK too.
So, what's the reason Eclipse compiles code like this -- is it any compiler parameter defined or different preferences, or differences in compilers. How to handle (fix) this while developing in Eclipse and be sure that it will compile in javac
.
part of this is already explained in the Question Where does Eclipse find javac to compile a project? - Eclipse uses a different compiler
这篇关于Java:在Eclipse和javac中处理异常的泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!