服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java基础学习笔记之泛型

java基础学习笔记之泛型

2020-03-28 11:26hebedich Java教程

所谓泛型,就是变量类型的参数化。泛型是JDK1.5中一个最重要的特征。通过引入泛型,我们将获得编译时类型的安全和运行时更小的抛出ClassCastException的可能。在JDK1.5中,你可以声明一个集合将接收/返回的对象的类型。

泛型

将集合中的元素限定为一个特定的类型。

术语

  1. ArrayList<E> -- 泛型类型
  2. ArrayList -- 原始类型
  3. E -- 类型参数
  4. <> -- 读作"typeof"
  5. ArrayList<Integer> -- 参数化的类型
  6. Integer -- 实际类型参数

几点注意:

参数化类型和原始类型相互兼容

?
1
2
ArrayList collection1 = new ArrayList<Integer>();//通过,无warning
ArrayList<Integer> collection2 = new ArrayList();//通过,有warning

参数化类型不考虑类型参数的继承关系

?
1
2
ArrayList<String> collection3 = new ArrayList<Object>();//编译不通过
ArrayList<Object> collection4 = new ArrayList<String>();//编译不通过

但是

?
1
2
ArrayList collection5 = new ArrayList<Integer>();
ArrayList<String> collection6 = collection5;//编译通过

"?"通配符

"?"表示任意类型,使用"?"通配符可以引用各种参数化的类型,可以调用与参数化无关的方法(如size()方法),不能调用与参数化有关的方法(如add()方法)

通配符的扩展

限定通配符的上边界

?
1
2
ArrayList<? extends Number > collection1= new ArrayList<Integer >();//编译通过
ArrayList<? extends Number > collection2= new ArrayList<String>();//编译不通过

限定通配符的下边界

?
1
2
ArrayList<? super Integer > collection3= new ArrayList<Number >();//编译通过
ArrayList<? super Integer > collection4= new ArrayList<String>();//编译不通过

自定义泛型方法

C++模板函数

?
1
2
3
template <class T> T add(T x, T y){
  return (T)(x+y);
}

而java的泛型基本上完全在编译器中实现,用于编译器执行类型检查和类型判断,然后生成普通的非泛型的字节码,这种实现技术为“擦除”(erasure)。

"擦除"实例

泛型是提供给javac编译器使用的,限定集合的输入类型,编译器编译带类型说明的集合时会去掉“类型”信息。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class GenericTest {
  public static void main(String[] args) {
    new GenericTest().testType();
  }
 
  public void testType(){
    ArrayList<Integer> collection1 = new ArrayList<Integer>();
    ArrayList<String> collection2= new ArrayList<String>();
 
    System.out.println(collection1.getClass()==collection2.getClass());
    //两者class类型一样,即字节码一致
 
    System.out.println(collection2.getClass().getName());
    //class均为java.util.ArrayList,并无实际类型参数信息
  }
}

输出

true
java.util.ArrayList
使用反射可跳过编译器,往某个泛型集合加入其它类型数据。

只有引用类型才能作为泛型方法的实际参数 例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class GenericTest {
  public static void main(String[] args) {
    swap(new String[]{"111","222"},0,1);//编译通过
 
    //swap(new int[]{1,2},0,1);
    //编译不通过,因为int不是引用类型
 
    swap(new Integer[]{1,2},0,1);//编译通过
  }
 
  /*交换数组a 的第i个和第j个元素*/
  public static <T> void swap(T[]a,int i,int j){
    T temp = a[i];
    a[i] = a[j];
    a[j] = temp;
  }
}

但注意基本类型有时可以作为实参,因为有自动装箱和拆箱。 例子(编译通过了):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class GenericTest {
  public static void main(String[] args) {
    new GenericTest().testType();
    int a = biggerOne(3,5);
    //int 和 double,取交为Number
    Number b = biggerOne(3,5.5);
    //String和int 取交为Object
    Object c = biggerOne("1",2);
  }
  //从x,y中返回y
  public static <T> T biggerOne(T x,T y){
    return y;
  }
}

同时,该例还表明,当实参不一致时,T取交集,即第一个共同的父类。 另外,如果用Number b = biggerOne(3,5.5);改为String c = biggerOne(3,5.5);则编译报错:

Error:(17, 29) java: 不兼容的类型: 推断类型不符合上限
    推断: java.lang.Number&java.lang.Comparable<? extends java.lang.Number&java.lang.Comparable<?>>
    上限: java.lang.String,java.lang.Object
但是有一点没搞清楚,我在IDEA里面单步调试,发现结果如下图: 泛型调试截图-1 不知道b为什么是Double类型的(但直接Double b接收返回值会编译报错)。不知道跟IDE有没有关系,是不是IDE在debug时会显示这个对象最精确的类型?

类型参数的类型推断

编译器判断泛型方法的实际类型参数的过程称为类型推断。

当某个类型变量只在整个参数列表的所有参数和返回值中的一处被应用了,那么根据调用方法时该处的实际应用类型来确定。即直接根据调用方法时传递的参数类型或返回值来决定泛型参数的类型。 例如:

?
1
swap(new String[3],1,2) -> static <E> void swap(E[]a,int i,int j)

当某个类型变量在整个参数列表的所有参数和返回值中的多处被应用了,如果调用方法时这么多处的实际应用类型都 对应同一种类型,则泛型参数的类型就是该类型。 例如:

?
1
add(3,5) -> static <T> T add(T a,T b)

当某个类型变量在整个参数列表的所有参数和返回值中的*多处被应用了,如果调用方法时这么多处的实际应用类型 对应不同的类型,且没有返回值,则取多个参数中的最大交集类型,即第一个公共父类。 例如:

?
1
fill(new Integer[3],3.5) -> static <T> void fill(T a[],T v)

该例子实际对应的类型就是Number,编译通过,运行出问题。

当某个类型变量在整个参数列表的所有参数和返回值中的多处被应用了,如果调用方法时这么多处的实际应用类型对应不同的类型,且使用有返回值,则优先考虑返回值的类型
例如:

?
1
int x = add(3,3.5) -> static <T> T add(T a,T b)

上例编译报错,x类型改为float也报错,改为Number成功。

参数类型的类型推断具有传递性
例子:

copy(new Integer[5],new String[5]) -> static <T> void copy(T []a,T []b)

该例推断实际参数类型为Object,编译通过.

copy(new ArrayList<String>,new Integer[5]) -> static <T> void copy(Collection<T>a,T[]b)

该例则根据参数化的ArrayList类实例将类型变量直接确定为String类型,编译报错。

自定义泛型类

例子

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class GenericDao<T>{
  public void add(T x){
  }
 
  public T findById(int id){
    return null;
  }
 
  public void delete(T obj){
  }
 
  public void delete(int id){
  }
 
  public void update(T obj){
  }
 
  public T findByUserName(String name){
    return null;
  }
 
  public <T> Set<T> findByConditions(String where){
    return null;
  }
 
}

注意:当一个变量被声明为泛型时,只能被实例变量和方法调用(还有内嵌类型),而不能被静态变量和静态方法调用。因为静态成员是被所参数化的类所共享的,所以静态成员不应该有类级别的类型参数。

泛型方法和泛型类的比较

例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class A<T>(){
  //泛型类的成员方法,该T受A后面的T的限制
  public T memberFunc(){
    return null;
  }
  //泛型方法,这里的T和和类A的T是不同的
  public static <T> T genericFunc(T a){
    return null;
  }
  public static void main(String[] args) {
    //编译不通过
    //Integer i = A<String>().findByUserName("s");
 
    //编译通过
    Set<Integer> set= A<String>().findByConditions("s");
  }
}

这里Integer i = A<String>().findByUserName("s");会编译报错:

Error:(35, 61) java: 不兼容的类型: java.lang.String无法转换为java.lang.Integer

由这个例子可知,泛型方法的T和和类A的T是不同的。

泛型和反射

通过反射获得泛型的实际类型参数

把泛型变量当成方法的参数,利用Method类的getGenericParameterTypes方法来获取泛型的实际类型参数 例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class GenericTest {
  public static void main(String[] args) throws Exception {
    getParamType();
  }
 
   /*利用反射获取方法参数的实际参数类型*/
  public static void getParamType() throws NoSuchMethodException{
    Method method = GenericTest.class.getMethod("applyMap",Map.class);
    //获取方法的泛型参数的类型
    Type[] types = method.getGenericParameterTypes();
    System.out.println(types[0]);
    //参数化的类型
    ParameterizedType pType = (ParameterizedType)types[0];
    //原始类型
    System.out.println(pType.getRawType());
    //实际类型参数
    System.out.println(pType.getActualTypeArguments()[0]);
    System.out.println(pType.getActualTypeArguments()[1]);
  }
 
  /*供测试参数类型的方法*/
  public static void applyMap(Map<Integer,String> map){
 
  }
 
}

输出结果:

?
1
2
3
4
java.util.Map<java.lang.Integer, java.lang.String>
interface java.util.Map
class java.lang.Integer
class java.lang.String

延伸 · 阅读

精彩推荐