博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
泛型(Generics Types)学习笔记
阅读量:4192 次
发布时间:2019-05-26

本文共 2319 字,大约阅读时间需要 7 分钟。

Generics Types 泛型学习笔记 1

作者:冰云 icecloud(AT)sina.com

BLOG:

时间:2004.02.15

 

版权声明:

本文由冰云完成,首发于CSDN,作者保留中文版权。

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

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

Generics in the Java Programming Language

Gilad Bracha

Febrary 16, 2004.

 

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

 

 

    public interface List {

       void add(E x);

       Iterator iterator();

    }

    public interface Iterator {

       E next();

       boolean hasNext();

    }

   

 

基本的调用

 

    List myIntList = new LinkedList ();

    myIntList.add(new Integer(0));

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

 

 

 

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

原文: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 iterator();

    }

 

 

 

 

2 List 不是List的子类型

 

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

 

    List ls = new ArrayList ();

    List lo = ls;

   

    lo.add(new Object();

    String s = ls.get(0);

 

 

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

原文: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 is a subtype of G .

 

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

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

 

    void printCollection(Collection c){

       for (Object e:c)

           System.out.println(e);

    }

 

其中,Collection 表示其可以是任何类型。如Collection Collection Collection 是他们的父类型。

 

Note 3:Collection 是所有种类的子类。而不是Collection。这叫做“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

中的每个对象都是Shape类型。

 

    public void drawAll(List shapes) {

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

    }

    drawAll(List

circles);

 

 

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

 

 

    public void drawAll(List extends Shape> shapes){

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

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

    }  

 

 

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

原文:We have replaced the type List with List . 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.

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

你可能感兴趣的文章
阿里五年Java程序员的总结,献给还在迷茫中的你!
查看>>
程序员身上有异味,同事为什么都不会直接告诉他?
查看>>
大数据折射算法“歧视”?王思聪微博抽奖113位,仅有一位男性
查看>>
Java、C、C+ +、PHP、Python分别用来开发什么?一篇文章告诉你!
查看>>
Linux-SHELL常用命令
查看>>
Linux-网络运维基础
查看>>
Verilog编程网站学习——门电路、组合电路、时序电路
查看>>
android——学生信息显示和添加
查看>>
Android——ImageSwitcher轮流显示动画
查看>>
Android——利用手机端的文件存储和SQLite实现一个拍照图片管理系统
查看>>
图像调优1:清晰度相关参数MTF,SFR,MTF50,MTF50P 以及TVL的概念以及换算说明
查看>>
图像调优3: CCM参数的标定
查看>>
最长回文子串(Go,LeetCode)
查看>>
.Net Remoting配置文件的用法
查看>>
Tomcat性能调整优化
查看>>
利用SQL Server 2005减轻生产服务器优化负荷
查看>>
优化MYSQL服务器
查看>>
Exchange磁盘性能优化
查看>>
Apusic应用服务器的性能调节_JVM优化
查看>>
Apache重负荷服务器应如何优化?
查看>>