侧边栏壁纸
博主头像
小顺

一帆风顺 ⛵️⛵️⛵️

  • 累计撰写 70 篇文章
  • 累计创建 0 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

泛型通配符理解

小顺
2021-08-13 / 0 评论 / 0 点赞 / 29 阅读 / 118 字

package com.apesblog.day_25;

import java.util.ArrayList;
import java.util.List;

public class Test1 {

public static void main(String[] args) {
    List<? extends People> list1;
    List<? super People> list2;
    List<Object> objectlist = new ArrayList<Object>();
    List<People> peoplelist = new ArrayList<People>();
    List<Student> studentlist = new ArrayList<Student>();

    list1 = objectlist;//报错
    list1 = peoplelist;
    list1 = studentlist;

    list2 = objectlist;
    list2 = peoplelist;
    list2 = studentlist;//报错

    list1.add(null);
    list1.add(new People());//报错
    list1.add(new Student());//报错

    list2.add(null);
    list2.add(new People());
    list2.add(new Student());
    list2.add(new Object());//报错

}

}

class People {
}

class Student extends People {
}

0

评论区