类型变量和参数的定义 [英] Definition of type variable and parameter

查看:241
本文介绍了类型变量和参数的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Java中的泛型,使用 Java语言规范,第三版。在 4.6擦除类型擦除定义。在删除一个类型变量时,它说

I am reading up on generics in Java using the Java Language Specification, Third edition. In section "4.6 Erasure" type erasure is defined. On the erasure of a type variable it says


擦除类型变量(§4.4)是删除其最左边界。 / p>

The erasure of a type variable (§4.4) is the erasure of its leftmost bound.

这使我对类型变量类型参数之间的区别感到困惑。因为 4.4类型变量部分包含定义: TypeParameter:TypeVariable TypeBound 其中界限是可选的。但是也许你可以通过它出现的类型参数来识别一个类型变量,因为一个类型变量只能出现在一个上下文中,然后一个类型变量的最左边界被定义为其相应类型参数的最左边界或者 Object 以防在类型参数中出现明确的限制吗?

This confuses me a bit regarding the distinction between type variable and type parameter since section "4.4 Type Variables" has the definition: TypeParameter: TypeVariable TypeBound where the bound is optional. But perhaps you can identify a type variable with the type parameter it appears in since a type variable can only (?) appear in one "context" and then a type variable's leftmost bound is defined as the leftmost bound of its corresponding type parameter or Object in case there is no explicit bound appearing in the type parameter ?

推荐答案


如果没有绑定类型变量,则假定为Object。

If no bound is given for a type variable, Object is assumed.

找到您的链接。这意味着给定 FirstClass< T extends String> SecondClass< V> ,您可以得到:

Found in your link. That means that given FirstClass<T extends String> and SecondClass<V> you get that:


  1. 类: FirstClass 类型参数: T extends String 。类型变量: T 。类型绑定:字符串

  2. 类: SecondClass 类型参数: V 类型变量: V 。类型绑定:默认为 Object

  1. Class: FirstClass Type Parameter: T extends String. Type Variable: T. Type Bound: String.
  2. Class: SecondClass Type Parameter: V Type Variable: V. Type Bound: defaults to Object.

strong>通过类型参数类型变量类型绑定我不是指语法规则,而是概念。因此, extends 就是关键字。

By Type Parameter, Type Variable and Type Bound I do not mean the grammar rule, but the concept. Therefore, extends is just the keyword.

关于最左边界,您可以在同一个链接中找到答案,在第一个引号之后有两个句子:

About the leftmost bound you can find the answer in the same link, two sentences after the first quote:


绑定中的类型顺序仅仅是重要的,因为类型变量的删除由第一种类型在其绑定中,而类类型或类型变量可能只出现在第一个位置。

The order of types in a bound is only significant in that the erasure of a type variable is determined by the first type in its bound, and that a class type or type variable may only appear in the first position.

这篇关于类型变量和参数的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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