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

推荐订阅源

Jina AI
Jina AI
C
Cybersecurity and Infrastructure Security Agency CISA
美团技术团队
J
Java Code Geeks
博客园 - 聂微东
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
雷峰网
雷峰网
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
IT之家
IT之家
Last Week in AI
Last Week in AI
Recent Announcements
Recent Announcements
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
The Exploit Database - CXSecurity.com
I
Intezer
V
Vulnerabilities – Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
T
Tenable Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Project Zero
Project Zero
H
Hacker News: Front Page
SecWiki News
SecWiki News
L
LINUX DO - 最新话题
Hacker News: Ask HN
Hacker News: Ask HN
Forbes - Security
Forbes - Security
C
CERT Recently Published Vulnerability Notes
T
Threatpost
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V2EX - 技术
V2EX - 技术

博客园 - 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》