了解Java泛型。输入参数约定 [英] Understanding Java generics. Type parameter conventions

查看:76
本文介绍了了解Java泛型。输入参数约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


最常用的类型参数名称是:

电子元素(广泛用于Java集合框架)



K - Key

数字



V - 价值

S,U,V等 - 第2,3,4类


我似乎不太明白每个字母对应的字母。我知道每个字母都只是一个约定,但是第二,第三和第四类型究竟意味着什么?我应该什么时候使用?在他们的官方教程网站上,它没有提供进一步的信息。

解决方案

一些例子: 地图< K,V> :地图通常将 V 分配给 K EYS。

  • 列表< E> :列表包含电子 lements。这是一个被称为元素的约定。另一方面, T 在这里也是可以接受的。

  • Formatter< T> :格式化程序可以格式化任何 T ype。这不是一个真正的元素,也不是一个关键,也不是一个价值,所以 T 在这里是正确的。

  • 三元组< T,U,V> :任意类型的三元组。由于类型定义对后面将填充的类型没有任何了解,因此它只使用 T 作为第一种类型,接下来是按字母顺序排列的下一个字母。


  • The most commonly used type parameter names are:

    E - Element (used extensively by the Java Collections Framework)

    K - Key

    N - Number

    T - Type

    V - Value

    S,U,V etc. - 2nd, 3rd, 4th types

    I don't seem to quite understand what exactly does every letter correspond to. I understand that each letter represents just a convention but what does 2nd, 3rd and 4th type mean exactly? When should I use what? On their official tutorials website it doesn't give further information.

    解决方案

    Some examples:

    • Map<K, V>: A map usually assigns Values to Keys. These are special kinds of types, so they are used here.
    • List<E>: A list contains Elements. It's a convention that they are called elements. On the other hand, T would also be acceptable here.
    • Formatter<T>: A formatter can format any Type. It's not really an element, nor a key, nor a value, so T is the correct letter here.
    • Triplet<T, U, V>: A triplet for arbitrary types. Since the type definition does not know anything about the types that will be filled in later, it uses just the T for the first type, followed by the next letters in alphabetical order.

    这篇关于了解Java泛型。输入参数约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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