什么是最左边的泛型类型或方法的意思,为什么选择这种策略进行类型擦除? [英] What is meant by left most bound for generic type or a method and why was this policy chosen for type erasure?

查看:152
本文介绍了什么是最左边的泛型类型或方法的意思,为什么选择这种策略进行类型擦除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 Angelika Langer的一般常见问题



删除类型参数


当编译器找到泛型类型或方法,它
删除所有出现的类型参数,并用
替换它们的最左边界,或者在没有指定bound的情况下输入Object。




编辑:正如我在这种情况下理解的那样,最左边界指的是它的意思是字面意思,即在边界列表中最左边。我想了解为什么选择此政策?在这种情况下,最左边不是技术术语。它的字面意思是最左边。它仅仅意味着当类型参数有如下几个边界(限制):

  T extends Callable< Long> &安培;可运行的

编译器替换所有类型参数 T










$ b $ >这意味着,就像在Angelika的例子中那样:

  private T task1,task2; 

被替换为:

  private可调用task1,task2; 


From Angelika Langer's generic FAQ:

Eliding type parameters.

When the compiler finds the definition of a generic type or method, it removes all occurrences of the type parameters and replaces them by their leftmost bound, or type Object if no bound had been specified.

Edit: As I understood in this case leftmost bound means what it means literally i.e farthest to the left in the list of bounds. I want to understand why was this policy chosen?

解决方案

In this case "leftmost" is not technical terminology. It literally means "farthest to the left". It simply means that when the type parameter has several bounds (restrictions) like this:

T extends Callable<Long> & Runnable

the compiler replaces all occurances of the type parameter T with the bound that is farthest to the left (in this case that's Callable<Long>):

Meaning that, as in Angelika's example, this:

private T task1, task2; 

is replaced with this:

private Callable task1, task2; 

这篇关于什么是最左边的泛型类型或方法的意思,为什么选择这种策略进行类型擦除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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