`
冰云
  • 浏览: 144074 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
社区版块
存档分类
最新评论

泛型(Generics Types)学习笔记<一>

阅读更多

Generics Types 泛型学习笔记 1

作者:冰云 icecloud(AT)sina.com
BLOG: http://icecloud.51.net

时间:2004.02.15

 

版权声明:

本文由冰云完成,首发于CSDN,作者保留中文版权。
未经许可,不得使用于任何商业用途。
欢迎转载,但请保持文章及版权声明完整。
如需联络请发邮件:icecloud(AT)sina.com

Java 1.5 提供了泛型支持。前几天,Sun发布了一篇tutorial。以下是对该tutorial的学习笔记。

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

Generics in the Java Programming Language

Gilad Bracha

Febrary 16, 2004.

 

1 泛型编译后实际上会产生不同的类型声明

 

 

    public interface List<E> {

       void add(E x);

       Iterator<E> iterator();

    }

    public interface Iterator<E> {

       E next();

       boolean hasNext();

    }

   

 

基本的调用

 

    List<Integer> myIntList = new LinkedList<Integer>();

    myIntList.add(new Integer(0));

    Integer x = myIntList.iterator().next();

 

 

 

Note 1: 每个泛型声明仅编译一次,并且为每个不同类型产生一个单独的文件。就像旧的类一样。比如,可能会有List<Integer>类和List<Boolean>类被产生。

原文:A generic type declaration is compiled once and for all, and turned into a single class file, just like an ordinary class or interface declaration.

 

编译后,相当于产生了下面的类

 

    public interface IntegerList {

       void add(Integer x);

       Iterator<Integer> iterator();

    }

 

 

 

 

2 List<String>不是List<Object>的子类型

 

以下语句将无法通过编译,出现错误在第2行,因为lo不是ls的父类型。

 

    List<String> ls = new ArrayList<String>();

    List<Object> lo = ls;

   

    lo.add(new Object();

    String s = ls.get(0);

 

 

Note 2: 一般来说,如果FooBar的子类型,G是泛型类型,那么G<Foo>不是G<Bar>的子类型。书上说这是泛型学习的最大难点。

原文:In general, if Foo is a subtype (subclass or subinterface) of Bar, and G is som generic type declaration, it is not the case that G<Foo> is a subtype of G<Bar>.

 

3泛型的父类型是<?>,通配符类型。

一个用到Collection的泛型方法如下,其中for循环用了新式的方法:

 

    void printCollection(Collection<?> c){

       for (Object e:c)

           System.out.println(e);

    }

 

其中,Collection<?>表示其可以是任何类型。如Collection<Integer>Collection<String>Collection<?>是他们的父类型。

 

Note 3:Collection<?>是所有种类的子类。而不是Collection<Object>。这叫做“wildcard type”通配符类型。

原文:What is the supertype of all kinds of collections? It’s written Collection<?> (pronounced “collection of unknown”), that is, a collection whose element type matches anything. It’s called a wildcard type for obvious reason.

 

4 Bounded wildcards.有限制的通配符类型

 

很不幸,下面的方法调用无法成功。虽然List<Circle>中的每个对象都是Shape类型。

 

    public void drawAll(List<Shape> shapes) {

       for(Shap s:shapes)s.deaw();

    }

    drawAll(List<Circle> circles);

 

 

通过bounded wildcard可以解决:然而这样做会带来一点代价。即,它是只读的,你不能再向List中添加新元素。

 

 

    public void drawAll(List<? extends Shape> shapes){

       // 下面的调用是错误的,只能读

       shapes.add(0,new Circle());

    }  

 

 

Note 4: <? extends Class>是一种限制通配符类型,它可以接受所有<Class>以及Class的子类型。然而调用代价是,只读访问。

原文:We have replaced the type List<Shape> with List<? extends Shape>. Now drawAll() will accept lists of any subclass of Shape. It is an example of a bounded wildcard. It is now illegal to write into shapes in the body of the method.

这是三分之一左右,地铁上看的,后面的还没看。



分享到:
评论

相关推荐

    C#知识要点

    例如,Func&lt;T&gt;表示一个不带参数但返回一个值的方法,而Func&lt;T1, T2, TResult&gt;则表示一个接受两个参数并返回一个值的方法。 委托是支持事件驱动编程的基础。在C#中,事件是一种特殊的委托,它表示一个动作或一组动作...

    个人文档:日常工作学习笔记

    "个人文档:日常工作学习笔记"这样的资源就是一个宝贵的积累,其中可能包含了开发者在使用TypeScript过程中遇到的问题、解决方案以及一些深入理解TypeScript的笔记。 TypeScript是JavaScript的一个超集,由微软开发...

    SCJP 1.4

    书中详细介绍了如何使用类型安全的列表(如`List&lt;T&gt;`)和映射(如`Map&lt;K, V&gt;`),以及如何迭代和操作参数化类型。 #### 关键小节: - 使用类型安全的列表 - 使用类型安全的映射 - 迭代参数化类型 - 接受参数化类型...

    15_王文峰C#ch09预习.zip

    9. **集合框架(Collection Framework)**:如List&lt;T&gt;、Dictionary&lt;TKey, TValue&gt;等,是C#中常用的数据结构,了解其工作原理和使用场景至关重要。 10. **异常处理(Exception Handling)**:通过`try-catch`语句,...

    typescript入门教程

    TypeScript入门教程是由xcatliu编写,他根据自身学习TypeScript的经历,整理出了一系列的学习笔记。这本教程从JavaScript程序员的角度出发,帮助初学者循序渐进地理解TypeScript,并提供了一个与官方手册不同的视角...

    ts-notes:关于官方TypeScript手册的注意事项

    例如,`Array&lt;T&gt;`就是一种泛型数组,其中`T`代表任意类型。 5. 函数类型:在TypeScript中,函数也是第一类公民,我们可以为函数定义类型,包括参数类型和返回类型。例如,`function add(a: number, b: number): ...

    C#教程 C#笔记 C#高级 C#知识点.rar

    - **泛型(Generics)**:提供类型安全的数据容器,减少类型转换,提高性能。 - **匿名方法与Lambda表达式**:简化代码,尤其在配合LINQ使用时,使得查询更加简洁。 3. **C#进阶特性** - **异步编程(Async/...

    CoreJava重点要点笔记

    ### Core Java 重点要点笔记解析 #### 一、Java编程语言的发展历史与主要特性 - **起源**: Java 最初源自 Sun 公司的一个名为 Green 的项目,该项目旨在为...对于初学者来说,这份笔记是学习 Java 编程的宝贵资源。

Global site tag (gtag.js) - Google Analytics