简体   繁体   English

java泛型中的“AnyType”是什么

[英]What is `AnyType` in java generics

What is AnyType in java. java中的AnyType是什么? When should i use it.我应该什么时候使用它。 For example TestRpn<AnyType extends Comparable<AnyType>> in this code snippet what is AnyType , and what kind of data it can have (ie, like Integer,Boolean)例如TestRpn<AnyType extends Comparable<AnyType>>在这个代码片段中AnyType是什么,它可以拥有什么样的数据(即,像 Integer,Boolean)

There is no predefined type named AnyType .没有名为AnyType预定义类型。 I am guessing you have come across a poorly named type parameter variable.我猜你遇到了一个命名不佳的类型参数变量。 The naming convention for type variables is suggested to be single uppercase letters , to avoid this type of confusion.类型变量的命名约定建议为单个大写字母,以避免此类混淆。

In your case, TestRpn<AnyType extends Comparable<AnyType>> should be rephrased as TestRpn<E extends Comparable<E>> , which in turn means that you can substitute any type for E that implements Comparable for it's own type.在您的情况下, TestRpn<AnyType extends Comparable<AnyType>>应该改写为TestRpn<E extends Comparable<E>> ,这反过来意味着您可以将实现 Comparable 的 E 替换为它自己的类型的任何类型。 Example: java.lang.String implements Comparable<String> , so TestRpn<String> is a valid parametrization of the above base type.示例: java.lang.String 实现Comparable<String> ,因此TestRpn<String>是上述基本类型的有效参数化。

AnyType is actually used in java7+. AnyType 实际上是在 java7+ 中使用的。 It can be said that its usage is same as that of E or T or something.可以说它的用法和E或者T什么的一样。 As the name suggests it can any type of generic data.顾名思义,它可以是任何类型的通用数据。 for example, when I implemented my generic linked list in Java, I used例如,当我在 Java 中实现我的通用链表时,我使用了

private Node<AnyType> head;

where the word AnyType stands for the generic type Node.其中 AnyType 一词代表泛型类型节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM