我在Java中看到,可以创建一个Class泛型和一个泛型方法.我还看到了使构造函数与Class一起通用的代码.我可以只使构造函数通用吗?如果是,如何调用构造函数?
是的你可以。
class Example {
public <T> Example(T t) {}
public static void main(String[] args){
// In this example the type can be inferred, so new Example("foo")
// works, but here is the syntax just to show you the general case.
Example example = new<String>Example("foo");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
92 次 |
| 最近记录: |