Java泛型中的通配符参数是什么?
泛型是Java中的一个概念,您可以通过它使类、接口和方法能够接受所有(引用)类型的参数。换句话说,它是一个允许用户动态选择方法、类构造函数接受的引用类型的概念。通过将类定义为泛型,您可以使其类型安全,即它可以作用于任何数据类型。
要定义一个泛型类,您需要在类名后的尖括号“<>”中指定正在使用的类型参数,您可以将其视为实例变量的数据类型并继续编写代码。
示例
class Student<T>{
T age;
Student(T age){
this.age = age;
}
public void display() {
System.out.println("Value: "+this.age);
}
}
public class GenericsExample {
public static void main(String args[]) {
Student<Float> std1 = new Student<Float>(25.5f);
std1.display();
Student<String> std2 = new Student<String>("25");
std2.display();
Student<Integer> std3 = new Student<Integer>(25);
std3.display();
}
}输出
Value: 25.5 Value: 25 Value: 25
通配符
您可以使用“?”代替泛型中的类型参数 (T),表示未知类型。您可以将通配符用作:
- 参数类型。
- 字段
- 局部字段。
通配符的唯一限制是您不能在调用泛型方法时将其用作泛型方法的类型参数。
Java 提供三种类型的通配符:上界通配符、下界通配符和无界通配符。
上界通配符
通配符中的上界类似于泛型中的有界类型。使用它,您可以启用将特定类的所有子类型用作类型参数。
例如,如果您想将 Collection 对象作为方法的参数,其类型参数为 Number 类的子类,您只需声明一个以 Number 类作为上界的通配符。
要创建/声明上界通配符,您只需在“?”后指定 extends 关键字,然后是类名。
示例
下面的 Java 示例演示了上界通配符的创建。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.HashSet;
public class UpperBoundExample {
public static void sampleMethod(Collection<? extends Number> col){
for (Number num: col) {
System.out.print(num+" ");
}
System.out.println("");
}
public static void main(String args[]) {
ArrayList<Integer> col1 = new ArrayList<Integer>();
col1.add(24);
col1.add(56);
col1.add(89);
col1.add(75);
col1.add(36);
sampleMethod(col1);
List<Float> col2 = Arrays.asList(22.1f, 3.32f, 51.4f, 82.7f, 95.4f, 625.f);
sampleMethod(col2);
HashSet<Double> col3 = new HashSet<Double>();
col3.add(25.225d);
col3.add(554.32d);
col3.add(2254.22d);
col3.add(445.21d);
sampleMethod(col3);
}
}输出
24 56 89 75 36 22.1 3.32 51.4 82.7 95.4 625.0 25.225 554.32 2254.22 445.21
如果将除 Number 子类类型的 Collection 对象作为参数传递给上述程序的 sampleMethod(),则会生成编译时错误。
示例
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.HashSet;
public class UpperBoundExample {
public static void sampleMethod(Collection<? extends Number> col){
for (Number num: col) {
System.out.print(num+" ");
}
System.out.println("");
}
public static void main(String args[]) {
ArrayList<Integer> col1 = new ArrayList<Integer>();
col1.add(24);
col1.add(56);
col1.add(89);
col1.add(75);
col1.add(36);
sampleMethod(col1);
List<Float> col2 = Arrays.asList(22.1f, 3.32f, 51.4f, 82.7f, 95.4f, 625.f);
sampleMethod(col2);
HashSet<String> col3 = new HashSet<String>();
col3.add("Raju");
col3.add("Ramu");
col3.add("Raghu");
col3.add("Radha");
sampleMethod(col3);
}
}编译时错误
UpperBoundExample.java:31: error: incompatible types: HashSet<String> cannot be converted to Collection<? extends Number> sampleMethod(col3); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error
下界通配符
上界通配符允许将特定类的所有子类型用作类型参数。
类似地,如果我们使用下界通配符,您可以将“?”的类型限制为特定类型或其超类型。
例如,如果您想将 Collection 对象作为方法的参数,其类型参数为 Integer 类的超类,您只需声明一个以 Integer 类作为下界的通配符。
要创建/声明下界通配符,您只需在“?”后指定 super 关键字,然后是类名。
下面的 Java 示例演示了下界通配符的创建。
示例
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Iterator;
public class LowerBoundExample {
public static void sampleMethod(Collection<? super Integer> col){
Iterator it = col.iterator();
while (it.hasNext()) {
System.out.print(it.next()+" ");
}
System.out.println("");
}
public static void main(String args[]) {
ArrayList<Integer> col1 = new ArrayList<Integer>();
col1.add(24);
col1.add(56);
col1.add(89);
col1.add(75);
col1.add(36);
sampleMethod(col1);
List<Object> col2 = Arrays.asList(22.1f, 3.32f, 51.4f, 82.7f, 95.4f, 625.f);
sampleMethod(col2);
}
}输出
24 56 89 75 36 22.1 3.32 51.4 82.7 95.4 625.0
如果将除 Integer 及其超类型类型的 Collection 对象作为参数传递给上述程序的 sampleMethod(),则会生成编译时错误。
示例
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Iterator;
import java.util.HashSet;
public class LowerBoundExample {
public static void sampleMethod(Collection<? super Integer> col){
Iterator it = col.iterator();
while (it.hasNext()) {
System.out.print(it.next()+" ");
}
System.out.println("");
}
public static void main(String args[]) {
ArrayList<Integer> col1 = new ArrayList<Integer>();
col1.add(24);
col1.add(56);
col1.add(89);
col1.add(75);
col1.add(36);
sampleMethod(col1);
List<Object> col2 = Arrays.asList(22.1f, 3.32f, 51.4f, 82.7f, 95.4f, 625.f);
sampleMethod(col2);
HashSet<Double> col3 = new HashSet<Double>();
col3.add(25.225d);
col3.add(554.32d);
col3.add(2254.22d);
col3.add(445.21d);
sampleMethod(col3);
}
}编译时错误
LowerBoundExample.java:34: error: incompatible types: HashSet<Double> cannot be converted to Collection<? super Integer> sampleMethod(col3); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error
无界通配符
无界通配符是可以使用未知类型的所有子类型,即任何类型(Object)都被接受为类型参数的通配符。
例如,如果您想接受一个对象类型的 ArrayList 作为参数,您只需声明一个无界通配符。
要创建/声明无界通配符,您只需将通配符字符“?”作为类型参数指定在尖括号内。
示例
下面的 Java 示例演示了无界通配符的创建。
import java.util.List;
import java.util.Arrays;
public class UnboundedExample {
public static void sampleMethod(List<?> col){
for (Object ele : col) {
System.out.print(ele+" ");
}
System.out.println("");
}
public static void main(String args[]) {
ArrayList<Integer> col1 = new ArrayList<Integer>();
col1.add(24);
col1.add(56);
col1.add(89);
col1.add(75);
col1.add(36);
sampleMethod(col1);
ArrayList<Double> col2 = new ArrayList<Double>();
col2.add(24.12d);
col2.add(56.25d);
col2.add(89.36d);
col2.add(75.98d);
col2.add(36.47d);
sampleMethod(col2);
}
}输出
24 56 89 75 36 24.12 56.25 89.36 75.98 36.47
如果您传递从数组创建的 List 对象(包含基本类型元素),则会生成编译时错误。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class UnboundedExample {
public static void sampleMethod(List<?> col){
for (Object ele : col) {
System.out.print(ele+" ");
}
System.out.println("");
}
public static void main(String args[]) {
ArrayList<Integer> col1 = new ArrayList<Integer>();
col1.add(24);
col1.add(56);
col1.add(89);
col1.add(75);
col1.add(36);
sampleMethod(col1);
ArrayList<Double> col2 = new ArrayList<Double>();
col2.add(24.12d);
col2.add(56.25d);
col2.add(89.36d);
col2.add(75.98d);
col2.add(36.47d);
sampleMethod(col2);
List<Object> col2 = Arrays.asList(22.1f, 3.32f, 51.4f, 82.7f, 95.4f, 625.f);
sampleMethod(col2);
}
}编译时错误
UnboundedExample.java:27: error: variable col2 is already defined in method main(String[]) List<Object> col2 = Arrays.asList(22.1f, 3.32f, 51.4f, 82.7f, 95.4f, 625.f); ^ 1 error
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP