File size: 825 Bytes
0284885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
package Generics_package5;

import java.util.ArrayList;

public class Generics_demo5 {
    public static void main(String[] args) {
        /*
            泛型不具备继承性,但是数据具备继承性
         */

        //创建集合的对象
        ArrayList<Ye> list1 = new ArrayList<>();
        ArrayList<Fu> list2 = new ArrayList<>();
        ArrayList<Zi> list3 = new ArrayList<>();

        //调用method方法
        method(list1);
        //error:  method(list2);
        //error:  method(list3);

        list1.add(new Ye());
        list1.add(new Fu());
        list1.add(new Zi());
    }

    //此时,泛型里面写的是什么类型,那么只能传递什么类型的数据
    private static void method(ArrayList<Ye> list1) {
    }
}

class Ye{}
class Fu extends Ye{}
class Zi extends Fu{}