List接口-Generic 泛型

 1 public class DemoClass4Generic {
 2     public static void main(String[] args) {
 3         //TODO
 4         ArrayList<Person> persons = new ArrayList<Person>();
 5 
 6         //泛型 也可以称为:类型参数
 7         Contaion15<User15> contaion15 = new Contaion15<User15>();
 8         contaion15.data = new User15();
 9 
10         /*
11         * 类型存在多态的使用
12         * 泛型没有多态,所以报错误
13         * */
14         //test(contaion15);  //错误
15 
16         test2(contaion15); //正确
17     }
18 
19     public static void test(Contaion15<Object> contaion15){
20         System.out.println("测试打印");
21     }
22 
23     public static void test2(Contaion15<User15> contaion15){
24         System.out.println("测试打印");
25     }
26 }
27 
28 class Person2{
29 
30 }
31 
32 class Contaion15<C>{
33     public C data;
34 }
35 
36 class User15{
37 
38 }

 

posted @ 2025-10-17 14:42  字节虫  阅读(12)  评论(0)    收藏  举报