您好我在调试时遇到过这个类,请有人指点我的意思。感谢。
class Something<P>{
private P someVariable;
}
//what does <P> mean here?
感谢。
答案 0 :(得分:9)
答案 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
是用于泛型的类型。
对于类型或实体类型,通常为T
或TEntity
。
只需将ArrayList<string>
视为类型为string
的示例。
答案 4 :(得分:2)
This is a Generic class definition. <P> is the place holder for an Object that get substituted at compile.