Java 中泛型方法的下界通配符是什么?

java 8object oriented programmingprogramming更新于 2025/11/20 17:48:17

泛型是 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 对象作为方法的参数,并且该参数的类型为 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 及其父类型之外的其他类型的集合对象作为参数传递给上述程序的 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

相关文章