一、泛型概念
泛型是一种类型的参数化,也就是说所操作的数据类型被指定为一个参数。一般运用在集合类中比较多,就是一个集合在没有泛型规定的时候,他是可以放置任何类型的,如果规定好了泛型就只能放置该类了。他为了解决这样一个问题,就是在没有类型规定的前提下,这个集合类可以防止任何类型,这样在编译期他是没问题的,但是在运行期他可能报错,这样就不符合提早暴露问题的原则。
泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。
二、泛型原理
在编译的时候进行泛型擦除,在运行时不会有类型化参数。
使用泛型时指定的类型只在编译期生效,在编译后会将所有的类型参数擦除到它的第一个边界,未指定边界的情况下擦除为 Object
。
三、泛型的语法和示例
泛型可以用来修饰方法、类、接口;
- 泛型方法
可以单独为方法声明泛型,而这个类不必是泛型类。定义泛型方法,只需要将泛型参数列表置于返回值之前。
泛型方法的模板:
/**
* 泛型方法的基本介绍
* @param tClass 传入的泛型实参
* @return T 返回值为T类型
* 说明:
* 1)public 与 返回值中间<T>非常重要,可以理解为声明此方法为泛型方法。
* 2)只有声明了<T>的方法才是泛型方法,泛型类中的使用了泛型的成员方法并不是泛型方法。
* 3)<T>表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T。
* 4)与泛型类的定义一样,此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型。
*/
public <T> T genericMethod(Class<T> tClass)throws InstantiationException ,
IllegalAccessException{
T instance = tClass.newInstance();
return instance;
}
泛型方法的代码示例:
public class GenericMethod {
public <T> T getParam(T parameter) {
System.out.println("parameter = " + parameter);
return parameter;
}
public static void main(String[] args) {
GenericMethod generic = new GenericMethod();
generic.getParam("aaa");
}
}
静态方法也可以使用泛型,但是需要注意的是泛型方法中无法访问泛型类中泛型,其语法为:
public <T> T getParam(T parameter) {
System.out.println("parameter = " + parameter);
return parameter;
}
- 泛型类
在类名之后使用尖括号声明类型参数,声明的类型参数可以像普通类型一样用在类型声明处使用,到使用时再决定其具体类型,然后编译器会帮我们处理一些类型类型转换的细节。
public class GenericClass<T> {
T t;
}
泛型类示例:
public class GenericClass<T> {
public T value;
public GenericClass(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public static void main(String[] args) {
GenericClass<String> c = new GenericClass<String>("aaa");
String value = c.getValue();
System.out.println(value);
GenericClass<Integer> c1 = new GenericClass<Integer>(111);
Integer value1 = c1.getValue();
System.out.println(value1);
}
}
另外,可以通过 extends
显示的声明泛型类中类型参数的上界,若没有声明那么上界就是 Object
。声明类上界后,在使用该泛型类时指定的类型只能为上界或其子类。
public class GenericClass2<T extends Number> {
T value;
public GenericClass2(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public static void main(String[] args) {
GenericClass2<Integer> g = new GenericClass2<Integer>(111);
System.out.println("g = " + g.getValue());
}
}
- 泛型接口
接口也可以声明为泛型,声明方式同泛型类一样。
泛型接口语法模板:
public interface IGenericTest<T> {
T getValue();
}
泛型接口示例:
public class IGenericTestImpl implements IGenericTest<String> {
@Override
public String getValue() {
return "aaa";
}
public static void main(String[] args) {
IGenericTestImpl impl = new IGenericTestImpl();
String value = impl.getValue();
System.out.println("value = " + value);
}
}
四、泛型的通配符
在使用泛型的时候,我们还可以为传入的泛型参数进行上下边界的限制,如:类型参数只准传入某种类型的父类或某种类型的子类。
<? extends T>表示泛型的上限,为泛型添加上边界,即传入的类型实参必须是指定类型或其子类型。
使用了<? extends T> 声明的实例变量只能调用没有参数的方法,即只能读取。
GenericClass<? extends Integer> integerGenericClass = new GenericClass<>(111);
Integer value = integerGenericClass.getValue();
System.out.println("value = " + integerGenericClass.value);
<? super T>表示泛型的下限,为泛型添加下边界,即传入的类型实参必须是指定类型或其父类型
使用了<? super T> 声明的实例变量必须调用有参数的方法,即只能写入。
GenericClass<? super Integer> integerGenericClass = new GenericClass<>(111);
integerGenericClass.setValue(222);
System.out.println("value = " + integerGenericClass.value);
<?>表示只能传入一种确定类型。
五、常见问题
静态的属性、静态方法、和静态内部类是无法使用类的泛型参数的。如果要使用static方法具有泛型能力,可以使用泛型方法。
public class Calculate<T> {
// 静态方法时无法使用T的,编译时就会报错
public static T add(T a, T b) {
T c = a + b;
}
}
参考资料
- Java 泛型最全指南:https://juejin.cn/post/6943770419820396580#heading-13
- Java 泛型:https://www.runoob.com/java/java-generics.html
- java 泛型详解-绝对是对泛型方法讲解最详细的,没有之一:https://www.cnblogs.com/coprince/p/8603492.html
- Java泛型详解——绝对是对泛型方法讲解最详细的,没有之一!:https://cloud.tencent.com/developer/article/1172844