惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
aimingoo的专栏
aimingoo的专栏
月光博客
月光博客
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Stack Overflow Blog
Stack Overflow Blog
T
Threatpost
AWS News Blog
AWS News Blog
小众软件
小众软件
美团技术团队
L
Lohrmann on Cybersecurity
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Jina AI
Jina AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
宝玉的分享
宝玉的分享
A
Arctic Wolf
M
MIT News - Artificial intelligence
Scott Helme
Scott Helme
爱范儿
爱范儿
The Cloudflare Blog
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
有赞技术团队
有赞技术团队
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
I
InfoQ
V
Visual Studio Blog
N
Netflix TechBlog - Medium
Latest news
Latest news
C
CERT Recently Published Vulnerability Notes
T
Tenable Blog
H
Heimdal Security Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 最新话题
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threat Research - Cisco Blogs
S
Secure Thoughts
The Hacker News
The Hacker News
S
Schneier on Security
博客园 - 【当耐特】
T
The Exploit Database - CXSecurity.com
博客园_首页
N
News | PayPal Newsroom
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园 - sharewind

zookeepr 单机伪集群配置 简单介绍相册网站的架构 Javascript 动态加载 CSS STYLE 元素 DOS:先进入 bat 文件的路径,然后执行 bat 文件,然后回到当前目录收藏 (转载)简洁、明晰!数据库设计三大范式应用实例剖析 (转载)tomcat5下jsp出现getOutputStream() has already been called for this 解决eclipse+myeclipse的Processing Dirty Regions错误 Eclipse格式化代码时不换行 与 自动换行 JavaScript笔记 - 对象继承的几种方式 (转载)oracle 10g 安装完成后,无法登陆EM的解决办法 VS 2008 下载及序列号 摄像机 碰撞检测 Windows系统必备的30个免费开源软件 (转载) VC常见数据类型转换详解 java中关于时间日期操作的常用函数 How To Connect to a SQL Server 2000 Named Instance with JDBC JavaScript对象与数组参考大全 SQL Server数据库开发的二十一条军规 数据库经典文章!(必备)
Java泛型通配符extends与super
sharewind · 2012-11-26 · via 博客园 - sharewind

关键字说明

●  ? 通配符类型

●  <? extends T> 表示类型的上界,表示参数化类型的可能是T 或是 T的子类

●  <? super T> 表示类型下界(Java Core中叫超类型限定),表示参数化类型是此类型的超类型(父类型),直至Object

extends 示例

    static class Food{}
static class Fruit extends Food{}
static class Apple extends Fruit{}
static class RedApple extends Apple{}

List<? extends Fruit> flist = new ArrayList<Apple>();
// complie error:
// flist.add(new Apple());
// flist.add(new Fruit());
// flist.add(new Object());
flist.add(null); // only work for null

List<? extends Frut> 表示 “具有任何从Fruit继承类型的列表”,编译器无法确定List所持有的类型,所以无法安全的向其中添加对象。可以添加null,因为null 可以表示任何类型。所以List 的add 方法不能添加任何有意义的元素,但是可以接受现有的子类型List<Apple> 赋值。

Fruit fruit = flist.get(0);

Apple apple = (Apple)flist.get(0);

由于,其中放置是从Fruit中继承的类型,所以可以安全地取出Fruit类型。

flist.contains(new Fruit());

flist.contains(new Apple());

在使用Collection中的contains 方法时,接受Object 参数类型,可以不涉及任何通配符,编译器也允许这么调用。

super 示例

List<? super Fruit> flist = new ArrayList<Fruit>();

flist.add(new Fruit());

flist.add(new Apple());

flist.add(new RedApple());

// compile error:

List<? super Fruit> flist = new ArrayList<Apple>();

List<? super Fruit> 表示“具有任何Fruit超类型的列表”,列表的类型至少是一个 Fruit 类型,因此可以安全的向其中添加Fruit 及其子类型。由于List<? super Fruit>中的类型可能是任何Fruit 的超类型,无法赋值为Fruit的子类型Apple的List<Apple>.

// compile error:

Fruit item = flist.get(0);

因为,List<? super Fruit>中的类型可能是任何Fruit 的超类型,所以编译器无法确定get返回的对象类型是Fruit,还是Fruit的父类Food 或 Object.

小结

extends 可用于的返回类型限定,不能用于参数类型限定。

super 可用于参数类型限定,不能用于返回类型限定。

带有super超类型限定的通配符可以向泛型对易用写入,带有extends子类型限定的通配符可以向泛型对象读取。——《Core Java》