class <p>在Java中意味着什么?</p>

时间:2012-12-10 21:18:44

标签: java generics class-template

  

可能重复:
  What does the <TYPE> in java mean?

您好我在调试时遇到过这个类,请有人指点我的意思。感谢。

class Something<P>{
 private P someVariable;
}

//what does <P> mean here? 

感谢。

5 个答案:

答案 0 :(得分:9)

这是通用的。它允许您编写适用于不同类型的代码。

试用本教程:

http://docs.oracle.com/javase/tutorial/java/generics/

答案 1 :(得分:3)

这是类模板的一个示例(尽管它在运行时被擦除)。通常它是类而不是类

。它允许您在编译时将类型注入类中。

例如,如果你做了

new Something<String>();

然后someVariable将是String类型。

如果你打电话

new Something();

然后我相信someVariable将是Object类型,因为它没有推断类型信息。通常,您的IDE会向您发出警告。

还描述了here

答案 2 :(得分:3)

这意味着它是一个通用类。 您可以通过更改代码

来创建泛型类型声明
  "public class Box" to "public class Box<T>"

有关详细信息,请参阅此参考: http://docs.oracle.com/javase/tutorial/java/generics/types.html

答案 3 :(得分:2)

P是用于泛型的类型。

对于类型或实体类型,通常为TTEntity

只需将ArrayList<string>视为类型为string的示例。

答案 4 :(得分:2)

This is a Generic class definition.

<P> is the place holder for an Object that get substituted at compile.