<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Dedian  
    -- 關(guān)注搜索引擎的開發(fā)
    日歷
    <2006年6月>
    28293031123
    45678910
    11121314151617
    18192021222324
    2526272829301
    2345678
    統(tǒng)計(jì)
    • 隨筆 - 82
    • 文章 - 2
    • 評論 - 228
    • 引用 - 0

    導(dǎo)航

    常用鏈接

    留言簿(8)

    隨筆分類(45)

    隨筆檔案(82)

    文章檔案(2)

    Java Spaces

    搜索

    •  

    積分與排名

    • 積分 - 66107
    • 排名 - 813

    最新評論

    閱讀排行榜

    評論排行榜

     

    When reading GData source code, you will find that there are lots of generic-style code in it, which is one of several extensions of JDK 1.5. If you are using java 1.5 compiler, it is surely deserved to get some ideas about generic. Be noticed that Java generic looks like C++ Temple, but is quite different.

    1. what is the idea of generic?
    To simply say, generic is an idea of parameterizing type, including class type and other data types.

    2. examples?
    -- We are familar with some container types, such as Collection. Here is an example for our former (Java 1.4 or before) typical usage:
    Vector myList = new Vector();
    myList.add(new Integer(100));
    Integer value = (Integer)myList.get(0);

    now it is better to write like this for type safety: (Eclipse IDE will display type safety warnings for above code if under java 1.5 compiler option)
    ??Vector<Integer> myList = new Vector<Integer>();
    ??myList.add(new Integer(100));
    ??Integer value = myList.get(0);

    -- the reason why write code like this is Class Vector has been defined as a generic:
    public Class Vector<E>
    {
    ??????void add(E x);
    ????? ......
    }

    -- when we see some angle brackets(invocations) shown in?declaration, that is a generic. The invocation is a parameterized type. to use this generic, we need specify an actual type argument. (such as Integer as above)

    3. trick in generic

    -- we know that the idea of generic makes some data type such as container more flexible or acceptable for inputting entries. But that will be also very tricky. To take container as an example of generic, one of tricks is?can we copy values from one container to another container? if you want to copy like following style, the answer is no.
    List<String> ls = new ArrayList<String>();
    List<Object> lo = ls; //compile time error!

    -- though we know String is a subtype of Object, and we can assign a value of String to an Object. But we can not assign a List of String to a List of Object as a whole part(like reference to a variable). The reason is we can access inner part of List(I mean element here, if List is as a simple data type such as Object, maybe we can do that), that will make List type unsafe. So, Java 1.5 complier will not let you do that.

    -- Look inside two styles of code in above examples(of 2), we might say that the older style looks more flexible, because myList can accept more data types besides Integer, but the new style in 1.5 can only take Integer values. Well, if we need more flexible, we apply wildcards for generic.

    4. Wildcards and bounded wildcards

    -- if we see something like Collection<?> c, there is a question mark in angle brackets. That is Wildcard, which means type is temporarily unknown but it will be replaced by any type.
    -- if we see something like Collection<? extends Number> c, that is bounded wildcard, which means the elements in Collection has a supertype bound. You can not put any other type whose supertype is not Number into this Collection.
    -- But, no matter wildcard or bounded wildcard, we can not put a specified type value in it, that is because wildcard means type is unknown, you can not give a value to unknown data type.
    -- So, what hell can wildcard be used for ? return back the flexible idea we mentioned before. We need apply wildcard to describe a flexible idea in definition or declaration, not to do real things.
    for example, we can define an method like this:
    void printCollection(Collection<?> c)
    {
    ??????for(Object e : c){System.out.println(e);}
    }
    see? that is flexible. You can call this function for any Collection. You can use elements in Collection<?>, just don't try to put something in it.
    -- So the question is, if we wanna that flexibility for our method, and we also need put something in it during the subroutine. How can we do? and then, we need use generic method

    5. Generic method
    -- that means method declaration can also be parameterized.
    -- example:
    ????public <T> void addCollection(List<T> objs, T obj)
    ? ?{
    ??????? objs.add(obj);
    ?? ?}

    6. when to use generic method and when to use wildcard ?
    -- if the type parameter is used only once, or it has no relationship to other arguments of method including the return type, then wildcard?is?better to use to decribe clearer and more concise meanings.
    -- otherwise, generic method should be used.
    example:
    class Collection
    {
    ??????public static <T, S extends T> void copy(List<T> dest, List<S> src){...}
    }
    can be better rewritten as :
    class Collection
    {
    ??????public static <T> void copy(List<T> dest, List<? extends T> src){...}
    }

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

    posted on 2006-06-23 09:39 Dedian 閱讀(1401) 評論(0)  編輯  收藏 所屬分類: Java Glossary
     
    Copyright © Dedian Powered by: 博客園 模板提供:滬江博客
    主站蜘蛛池模板: 国产一区在线观看免费| 久久亚洲中文字幕精品一区四 | 欧洲精品免费一区二区三区| 亚洲视频免费观看| 亚洲毛片免费视频| 中国人xxxxx69免费视频| 真实乱视频国产免费观看| 亚洲国产欧洲综合997久久| 色www永久免费视频| 在线看亚洲十八禁网站| 久久这里只精品热免费99| 亚洲gv白嫩小受在线观看| 免费激情网站国产高清第一页| 免费国产小视频在线观看| 亚洲伊人久久大香线蕉| 天堂在线免费观看| 亚洲欧洲一区二区| 国产福利电影一区二区三区,免费久久久久久久精 | 国产卡二卡三卡四卡免费网址| 搡女人免费视频大全| 最新亚洲卡一卡二卡三新区| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 国产免费不卡视频| 亚洲日韩国产成网在线观看| 美女被免费网站91色| 国产国拍精品亚洲AV片| 久久中文字幕免费视频| 亚洲色大成网站WWW久久九九| 久久99青青精品免费观看| 日本亚洲精品色婷婷在线影院| 国产美女无遮挡免费视频网站 | 黄色毛片免费网站| 国产亚洲精品国产| 最新欧洲大片免费在线| 又硬又粗又长又爽免费看 | 国内精品久久久久影院亚洲| 夜色阁亚洲一区二区三区| 亚洲免费观看视频| 亚洲精品动漫免费二区| 亚洲精品成人片在线播放| 国语成本人片免费av无码|