Java 泛型和“...”

发布于 2024-08-14 18:45:24 字数 164 浏览 4 评论 0原文

  1. TS 是什么意思?
  2. public void main(String...abc) ; ... 是什么意思? ... 也被称为泛型吗?
  1. What do T and S mean?
  2. public void main(String... abc) ; what does ... mean? Is ... called generic as well?

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

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

发布评论

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

评论(6

美人迟暮 2024-08-21 18:45:25

T和S意味着类本身不知道它们是什么类,但使用该类的事物知道。

以 java.util.List 为例。列表类不了解有关 T 的任何信息,也不对 T 做出任何假设。 使用 List 类的内容:

List<MyBean> l = new ArrayList<MyBean>();

了解其中的内容。

T and S means that the class itself does not know what classes they are, but things that use the class do.

Take java.util.List. The list class does not know anything about T and makes no assumptions about T. Things that use the List class:

List<MyBean> l = new ArrayList<MyBean>();

Know what's in it.

楠木可依 2024-08-21 18:45:24
  1. 这是参数化类型。您可以在此处使用任何标识符来表示某种对象类型。
  2. 那是 varargs。您可以传入单个字符串,或多个字符串,或一个字符串数组。
  1. That are parameterized types. You can use any identifier here to represent a certain object type.
  2. That are varargs. You can pass a single String, or multiple Strings, or a String array in.
荒岛晴空 2024-08-21 18:45:24

TS 是泛型类。它们可以是您想要的任何类型的课程。例如,Map 使用 K 作为键类,使用 V 作为值类。

Map<Integer, String> map = new HashMap<Integer, String>

至于String...,它表示任意数量的字符串参数。

T and S are generic classes. They can be any type of class that you want. For example, Map<K, V> uses the K for the key class and V for the value class.

Map<Integer, String> map = new HashMap<Integer, String>

As for the String..., it means any number of String parameters.

花开浅夏 2024-08-21 18:45:24
  1. 请阅读文档。简而言之,它们是类型参数,以便泛型类型和方法可以知道它们正在作用的对象类型。
  2. 这表明该方法可以接受可变数量的参数。请参阅可变参数。它基本上是一个数组周围的糖。
  1. Please read the documentation. Briefly, they are type parameters so that generic types and methods can know what type of objects they are acting on.
  2. That indicates that the method can accept a variable number of arguments. See varargs. It's basically sugar around an array.
酒解孤独 2024-08-21 18:45:24

Sun 的 Java 泛型文档有时可能有点难以理解,因此我尝试编写一个关于 Java 泛型的更简单的教程。您可以在这里找到它:

http://tutorials.jenkov.com/java-generics/索引.html

Sun's Java Generics documentation can be a bit hard to understand at times, so I tried to write a simpler tutorial on Java Generics. You can find it here:

http://tutorials.jenkov.com/java-generics/index.html

徒留西风 2024-08-21 18:45:24

补充:
String...String[] 几乎相同。
在方法方面是相同的,
在调用方有一个区别:编译器根据参数创建数组。

void method(String... args) {
    // args is an array: getClass() returns [java.lang.String
    if (args.length >  0) {
        System.out.println(args[0]);
...
    method();               // same as method(new String[0]);
    method("1", "2", "3");  // same as method(new String[] {"1", "2", "3"});

complementing:
String... is almost the same as String[].
On the method side it is the same,
on the calling side the're is a difference: the compiler creates the array from the parameters.

void method(String... args) {
    // args is an array: getClass() returns [java.lang.String
    if (args.length >  0) {
        System.out.println(args[0]);
...
    method();               // same as method(new String[0]);
    method("1", "2", "3");  // same as method(new String[] {"1", "2", "3"});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文