java gettype错误,Java泛型编译错误 - 类型< type>中的方法方法(Class< capture#1-of?extends Interface>)不适用于论...

博客探讨了一个关于Java泛型的编译错误问题,作者在工作中遇到一个无法优雅解决的类型转换问题。代码示例中展示了如何创建一个Bar类和Foo类,以及在Foo的main方法中出现的编译错误。尽管作者找到了临时解决方案,但仍然对编译器为何不允许该转换感到困惑。文章建议当遇到类似问题时,可以尝试用List替换泛型类来帮助理解类型安全问题,并提供了一个使用List引发类型不安全的例子。
摘要由CSDN通过智能技术生成

Last Thursday someone at work showed me a compile error that I wasn't able to fix in a clean way and it has been bothering me ever since.

The problem is generics related and I've reconstructed a simplified version of the code that generates the compile error. The error occurs in the very last line of code shown below.

I've been looking all over the interwebs but can't seem to find a decent explanation why the Java compiler doesn't accept the code. I guess that if it were to allow the code, it would be possible the create a class cast issue in Bar.operationOnBar(), but I don't see how.

Could someone please enlighten me why this doesn't compile?

public interface Interface {

}

public class Type implements Interface {

}

public class Bar {

public Bar(Class clazz) {

}

public void operationOnBar(Class arg){

}

}

public class Foo {

public Bar bar(Class clazz){

return new Bar(clazz);

}

public static void main(String[] args) {

Class extends Interface> extendsInterfaceClazz = Type.class;

new Foo().bar(extendsInterfaceClazz).operationOnBar(Type.class);

}

}

Compile Error on the second line of Foo.main():

The method operationOnBar(Class) in the type Bar is not applicable for the arguments (Class)

Btw. I've solved it by downcasting Type.class to Class, this way the compiler is unable to see that the generic type of Class is "Type" instead of "? extends Interface".

解决方案

A little advice: when you are not sure why compiler prohibits some generic-related conversion, replace generic classes in question with List. Then it would be easy to find an example that breaks type safety.

This replacement is correct since currently Java doesn't provide a way to conduct any a priory knowledge about possible behaviours of generic classes (i.e. it lacks a way to specify covariance and contravariance of generic classes in their declarations, as in C# 4 and Scala). Therefore Class and List are equivalent for the compiler with respect to their possible behaviours, and compiler has to prohibit conversions that can cause problems with List for other generic classes as well.

In your case:

public class Bar {

private List l;

public Bar(List l) {

this.l = l;

}

public void operationOnBar(List arg) {

l.addAll(arg);

}

}

List l1 = new ArrayList();

List extends Interface> l2 = l1;

List l3 = Arrays.asList(new Type2());

new Foo().bar(l2).operationOnBar(l3);

Type1 t = l1.get(0); // Oops!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值