[英]Java Generic Methods and constructors
I am new to java.I was going through a tutorial on java generics. 我是java的新手,我正在阅读有关Java泛型的教程。
Java Generics tutorial There is a section about declaring generic methods and constructors. Java泛型教程这里有一个有关声明泛型方法和构造函数的部分。
Methods and constructors can be generic if they declare one/more type variables.
public static <T>T getFirst (List<T> list)
This method accepts a reference to List and will return an object of type T. 此方法接受对List的引用,并将返回T类型的对象。
why is there a T after static? 为什么静态后有T? Is this a printing mistake.?
这是打印错误吗?
What does that mean? 这意味着什么?
I am a beginner in java an new to the concepts of generics. 我是Java的初学者,是泛型概念的新手。
The T
before the method name means that returns a value of type T
, where T is the template used in the method. 方法名称前的
T
表示返回类型T
的值,其中T是方法中使用的模板。 If your method will return an int
, it would look like: 如果您的方法将返回一个
int
,则它将类似于:
public static <T> int getFirst (List<T> list)
T
java.lang.Class
Type参数,添加了此type参数。它只是向编译器说类型检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.