The Java™ Tutorials — Generics :Wildcards 通配符

The Java™ Tutorials — Generics :Wildcards 通配符

原文地址:https://docs.oracle.com/javase/tutorial/java/generics/wildcards.html

关键点

  • 通配符的适用范围: 
    • 参数类型
    • 字段类型
    • 局部变量类型
    • 返回值类型(但返回一个具体类型的值更好)

全文翻译

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

泛型中的问号符“?”名为“通配符”。它表示了一个位置类型。通配符可在很多场合中使用:作为一个参数、字段或者局部变量的类型;在某些时候也可作为一个返回值类型(虽然返回一个具体类型的值是更好的编程实践)。通配符从未被用于作为一个泛型方法调用、泛型类实例创建或是一个超类的类型变量。

The following sections discuss wildcards in more detail, including upper bounded wildcards, lower bounded wildcards, and wildcard capture.

下面的章节会对通配符做更深刻的讨论,包括受上下限控制的通配符和通配符匹配。

你可能感兴趣的:(java,泛型)