2.2 Wildcards with extends

前面介绍过List<Integer>不是List<Number>的子类,即前者不能替换后者,

java使用? extends [classname]语法,即List<Integer>

可以替换 List<? extends Number>

看两段代码:

package java_generics_collections.chap2;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List; /**
* Created by jintaox on 2016/3/21.
*/
public class Test_2_1 {
@Test
public void test_01() throws Exception {
List<Integer> ints = new ArrayList<>();
ints.add(3);
ints.add(4);
// List<Number> nums = ints;//compile error
// nums.add()
}
}

这是上一节介绍的内容,即List<Integer> 不能替换

List<Number>,语法编译不过,原因也介绍了。

下面这段代码:

package java_generics_collections.chap2;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List; /**
* Created by jintaox on 2016/3/22.
*/
public class Test_2_2 {
@Test
public void test_01() throws Exception {
List<Integer> ints = new ArrayList<>();
ints.add(1);
ints.add(12);
List<? extends Number> nums = ints;//compile pass
// nums.add(2.3);//compile error
}
}

可以看出List<Integer> 可以替换

List<? extends Number>

但是nums.add(2.3)是不允许的,还是那个原因,nums调用的实际是ints的add()方法(多态),

add(Integer i),因此,此处放一个double是不能编译通过的。

好了。

加油,我要做技术专家。

Java Generics and Collections-2.2的更多相关文章

  1. python 中的sort 和java中的Collections.sort()函数的使用

    x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...

  2. Java Concurrency - Concurrent Collections

    Data structures are a basic element in programming. Almost every program uses one or more types of d ...

  3. thinking in java Generics Latent typing

    The beginning of this chapter introduced the idea of writing code that can be applied as generally a ...

  4. Thinking in java——Generics

    ​Ordinary classes and methods work with specific types: either primitives or class types. If you are ...

  5. 【Java】Java中的Collections类——Java中升级版的数据结构【转】

    一般来说课本上的数据结构包括数组.单链表.堆栈.树.图.我这里所指的数据结构,是一个怎么表示一个对象的问题,有时候,单单一个变量声明不堪大用,比如int,String,double甚至一维数组.二维数 ...

  6. Java中的Collections类

    转载:https://blog.csdn.net/yangxingpa/article/details/80515963 从[Java]Java中的Collections类——Java中升级版的数据结 ...

  7. Java Generics and Collections-2.3

    2.3 Wildcards with super 这里就直接拿书上的例子好了,这是Collections里面的一个方法: public static <T> void copy(List& ...

  8. Java Generics and Collections-8.1

    8.1 Take Care when Calling Legacy Code 通常,泛型都是在编译时检查的,而不是运行时.便意识检查可以提早通知错误,而不至于到运行时才出问题. 但有时后编译时检查不一 ...

  9. java List 排序 Collections.sort() 对 List 排序

    class User { String name; String age;  public User(String name,String age){  this.name=name;  this.a ...

  10. 黑马程序员——JAVA基础之Collections和Arrays,数组集合的转换

    ------- android培训.java培训.期待与您交流! ---------- 集合框架的工具类:        Collections : 集合框架的工具类.里面定义的都是静态方法. Col ...

随机推荐

  1. xhtmlConformance与xhtml脚本呈现

    此配置节只有一个属性——mode,该特性为 ASP.NET 应用程序指定 XHTML 呈现模式.它包含三个值 要让此配置生效,需要把<pages>配置节中的controlRendering ...

  2. jquery easyui菜单树显示

    目前做了一个easyui项目需要显示多级菜单,菜单配置到数据库中,因此每级菜单都需要到数据库中取,用了jQuery EasyUI方便多了. 效果体验:http://hovertree.com/texi ...

  3. NPOI操作Excel辅助类

    /// <summary> /// NPOI操作excel辅助类 /// </summary> public static class NPOIHelper { #region ...

  4. webserver[实时查询当天的天气情况]

    1.webserver是什么? 日常生活中经常会使用到webserver,注册时,会收到验证码,购买东西时,会收到短信,假如,A公司网站和B公司合作,那么A公司注册对的用户可以直接推送给B网站,那怎么 ...

  5. java判断输入的数是不是素数

    package test; import java.util.Scanner; //判断输入的数是不是素数 public class Test18 { public static void main( ...

  6. C标准头文件<string.h>

    里面主要包含了一些与字符串关联的函数的声明,这些函数有如下的命名规则: 以"mem"开头的函数操作任意的字符序列 以"strn"开头的函数操作非空字符序列 以& ...

  7. PHP常量

    常量的定义 在PHP中,常量的声明是通过define()函数来定义的,它也是对大小写敏感的,按照一般的习惯PHP常量总是大写的,且不能再命名的常量之前加上$符号,在这里详细介绍一下define()函数 ...

  8. ahjesus HttpQuery

    /// <summary> /// 有关HTTP请求的辅助类 /// </summary> public class HttpQuery { private static re ...

  9. B-Tree索引在sqlserver和mysql中的应用

    在谈论数据库性能优化的时候,通常都会提到“索引”,但很多人其实并没有真正理解索引,也没有搞清楚索引为什么就能加快检索速度,以至于在实践中并不能很好的应用索引.事实上,索引是一种廉价而且十分有效的优化手 ...

  10. ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的统一管理

    话不多说,直接进入主题. 需求:基于Http请求接收Json格式数据,返回Json格式的数据. 整理:对接收的数据与返回数据进行统一的封装整理,方便处理接收与返回数据,并对数据进行验证,通过C#的特性 ...