私有静态T cloneX(T x) -是什么意思?这里表示?

发布于 2024-08-28 08:49:04 字数 94 浏览 2 评论 0原文

在上面的声明中, 的作用是什么?

我想知道有 和没有的区别?它如何影响代码?

In the above declaration, what is the <T> for?

I would like to know the difference between having <T> and not having it? How does it affect the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

活雷疯 2024-09-04 08:49:04

此处指示参数隐含类型。所以:

public static <T> List<T> createList(T... args) {
  List<T> ret = new ArrayList<T>(Arrays.asList(args));
}

可以使用:

List<String> list = createList("one", "two", "three");

List<Integer> list2 = createList(1, 2, 3);

<T> here indicates the type is implied from the arguments. So:

public static <T> List<T> createList(T... args) {
  List<T> ret = new ArrayList<T>(Arrays.asList(args));
}

can be used:

List<String> list = createList("one", "two", "three");

or

List<Integer> list2 = createList(1, 2, 3);
原谅过去的我 2024-09-04 08:49:04

它只是意味着您将从您放入的方法中获得相同的类,以将其保存为对象,并且您必须始终进行强制转换。

it just means that you will get the same class out of that method that you're putting in, to save it being Object and you having to cast all the time.

疯了 2024-09-04 08:49:04

是您传递到该泛型方法的参数的类型。

The <T> is the Type of the parameter you're passing into that generic method.

欢你一世 2024-09-04 08:49:04

它是通用参数。如果你那么写

string s = ...;
clone(s); // will be expanded to string clone(string x)

It is generic parameter. If you write then

string s = ...;
clone(s); // will be expanded to string clone(string x)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文