Java编译器如何对下界有界的通配符执行类型擦除? [英] How does the Java compiler perform type erasure for lower bounded wildcards?

查看:116
本文介绍了Java编译器如何对下界有界的通配符执行类型擦除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用泛型类型编译源代码时,Java编译器自动执行类型擦除,用合适的原始类型替换泛型声明。



根据Oracle文档,擦除会替换上面的保税通配符<使用T扩展T> 。这符合动态多态性。



但是,如何擦除下限通配符< ;?看看每个类都有一个共同的超类(Object),使用它会失败的全部目的?

解决方案对于超级类型,它将擦除为Object类型。<?super T> 仅用于编译时验证


While compiling source code with generic types, the Java compiler automatically performs type erasure, replacing the generic declarations with suitable raw types.

As per the Oracle docs, this erasure replaces the upper bonded wildcard <? extends T> with T. This fits well with dynamic polymorphism.

But how is this erasure performed for lower bounded wildcard <? super T> seeing that every class has a common super class (Object), using which will defeat the whole purpose?

解决方案

For super type,it erases to Object type.<?super T> is only used for compile time verification

这篇关于Java编译器如何对下界有界的通配符执行类型擦除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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