泛型是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提供3种类型的通配符,即上限,下限,无界。
通配符中的上限与泛型中的有界类型相似。使用此功能,可以将特定类的所有子类型用作类型参数。
例如,如果要接受Collection对象作为方法的参数,而类型化的参数作为数字类的子类,则只需要声明一个以数字类为上限的通配符即可。
要创建/声明上限通配符,只需在“?”之后指定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子类的type以外的其他收集对象作为参数传递给上述程序的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以外的其他类型的收集对象及其超类型作为参数传递给上述程序的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
无界通配符是一种允许使用未知类型的所有子类型的通配符,即,任何类型(对象)都可以用作类型参数。
例如,如果要接受对象类型的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