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

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

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

    posts - 7, comments - 17, trackbacks - 0, articles - 0
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    4.1. Generic Types

    Generic types and methods are the defining new feature of Java 5.0. A generic typeis defined using one or more type variablesand has one or more methods that use a type variable as a placeholder for an argument or return type. For example, the type java.util.List<E> is a generic type: a list that holds elements of some type represented by the placeholder E. This type has a method named add(), declared to take an argument of type E, and a method named get(), declared to return a value of type E.

    4.1. 泛型

    泛類型和泛型方法是 Java5.0 中的新特性。一種泛類型用一個(gè)或多個(gè)泛型變量定義,可以有一個(gè)或多個(gè),泛型變量做參數(shù)占位符或做返回值的方法。例如,類型 java.util.List<E> 是一種泛類型:一個(gè) list ,它的元素類型是 E 這個(gè)占位符表示的類型。這個(gè)類型有一個(gè)叫 add() 的方法,有一個(gè)參數(shù)類型為 E ,有一個(gè)名叫 get() 的方法,返回一個(gè)類型為 E 的值。

    In order to use a generic type like this, you specify actual types for the type variable (or variables), producing a parameterized type such as List<String>.[1] The reason to specify this extra type information is that the compiler can provide much stronger compile-time type checking for you, increasing the type safety of your programs. This type checking prevents you from adding a String[], for example, to a List that is intended to hold only String objects. Also, the additional type information enables the compiler to do some casting for you. The compiler knows that the get( ) method of a List<String> (for example) returns a String object: you are no longer required to cast a return value of type Object to a String.

    為了可以象往常一樣使用泛類型,你需要為泛型變量(或是變量)指定一個(gè)實(shí)際的類型,產(chǎn)生一個(gè)參數(shù)化類型,就象 List<String> 。這樣做的原因是,為編譯器提供一個(gè)特定的類型信息,讓它可以在編譯期為你做類型檢查,這樣可以大大增加你程序的類型安全。例如,有一個(gè) List 打算容納 String 類型的對(duì)象,這種類型安全檢查阻止你增加一個(gè) String[] 的元素。而且,這附加的類型信息使編譯器幫你做些造型的活兒。例如,編譯器知道 List<String> get() 方法返回一個(gè) String 類型對(duì)象,你不再需要把 Object 類型的返回值造型成一個(gè) String 類型的對(duì)象。

    [1] Throughout this chapter, I've tried to consistently use the term "generic type" to mean a type that declares one or more type variables and the term "parameterized type" to mean a generic type that has had actual type arguments substituted for its type varaiables. In common usage, however, the distinction is not a sharp one and the terms are sometimes used interchangeably.

    [1] 整個(gè)這章,我們將統(tǒng)一使用這些術(shù)語。 " 泛類型 " :意味著一個(gè)類型,可以聲明一個(gè)或多個(gè)泛型變量。 " 參數(shù)化類型 " ,意味著一個(gè)(運(yùn)行期的)泛類型,表示它的泛型變量被實(shí)際類型做為參數(shù)值替換了。但在通常的應(yīng)用中,兩個(gè)術(shù)語的差別不是太明顯,有時(shí)還可以替換。

    The collections classes of the java.util package have been made generic in Java 5.0, and you will probably use them frequently in your programs. Typesafe collections are the canonical use case for generic types. Even if you never define generic types of your own and never use generic types other than the collections classes in java.util, the benefits of typesafe collections are so significant that they justify the complexity of this major new language feature.

    Java 5.0 中,包 java.util 中的 Collection 類都已經(jīng)被泛化了,你可能會(huì)在程序中頻繁的用到它們。類型安全的 Collection 是應(yīng)用泛類型的典范。可能你還從沒有定義過自己的泛類型,也從沒用過 Collection (在包 java.uitl 中)之外的泛類型,但類型安全的 Collection 的好處是顯而易見的,它將證實(shí)這個(gè)新的,復(fù)雜的,主要且重要的語言特性。

    We begin by exploring the basic use of generics in typesafe collections, then delve into more complex details about the use of generic types. Next we cover type parameter wildcards and bounded wildcards. After describing how to use generic types, we explain how to write your own generic types and generic methods. Our coverage of generics concludes with a tour of important generic types in the core Java API. It explores these types and their use in depth in order to provide a deeper understanding of how generics work.

    我們將從探索類型安全的 Collection 的基本泛化用法開始,然后深入泛類型用法更為復(fù)雜的細(xì)節(jié)。接下來,我們將覆蓋泛型參數(shù)通配符和邊界通配符。再說明怎么用泛類型,我們將闡述怎樣寫自己的泛類型和泛型方法。我們涉及的泛型知識(shí)包括了 Java API 中關(guān)于泛類型的主要部分。深入的探討了這些類型和他們的用法,為的是更深入的理解泛型是如何工作的。

    4.1.1 . Typesafe Collections

    4.1.1 . 類型安全的 Collection

    The java.utilpackage includes the Java Collections Framework for working with sets and lists of objects and mappings from key objects to value objects. Collections are covered in Chapter 5. Here, we discuss the fact that in Java 5.0 the collections classes use type parameters to identify the type of the objects in the collection. This is not the case in Java 1.4 and earlier. Without generics, the use of collections requires the programmer to remember the proper element type for each collection. When you create a collection in Java 1.4, you know what type of objects you intend to store in that collection, but the compiler cannot know this. You must be careful to add elements of the appropriate type. And when querying elements from a collection, you must write explicit casts to convert them from Object to their actual type. Consider the following Java 1.4 code:

    在包 java.util 中包含了 Java Collection 框架 (set list-- 關(guān)于對(duì)象、 map-- 關(guān)于關(guān)鍵字對(duì)象和值對(duì)象的對(duì) ) Collection 將在第五章討論。這里我們只討論有關(guān)在 Java 5.0 Collection 類中,用泛型參數(shù)標(biāo)識(shí) Collection 對(duì)象類型的內(nèi)容。這在 Java 1.4 或是更早的 Java 版本中,沒有這個(gè)用法。在沒有泛型應(yīng)用 Collection 的時(shí)候,需要程序員正確記得每個(gè) Collection 的每個(gè)元素的類型。當(dāng)我們建立一個(gè) Java 1.4 Collection 時(shí),你知道打算把什么樣的類型對(duì)象放入這個(gè) Collection 中,但是編譯器不知道這些。你增加元素時(shí)必須小心正確性。并且當(dāng)獲取一個(gè) Collection 的元素時(shí),你必須顯示的造型對(duì)象 Object 到它們實(shí)際的類型。思考在 Java 1.4 中的如下代碼:?
    ???
    太郁悶了,代碼總是對(duì)不齊,算了,完整的還是發(fā)個(gè)pdf吧,要看自己下了,
    不要忘了拍磚就行了!
    pdf修訂了一下




    評(píng)論

    # re: 自己翻譯的Java.In.A.Nutshell.5th中泛型一章,歡迎拍磚把文章砸的漂亮一些  回復(fù)  更多評(píng)論   

    2006-09-21 21:06 by 123bingbing
    老兄,有空到[url]www.mylinux.com.cn[/url]坐坐。那里挺專業(yè)的。

    # re: 自己翻譯的Java.In.A.Nutshell.5th中泛型一章,歡迎拍磚把文章砸的漂亮一些  回復(fù)  更多評(píng)論   

    2006-09-24 21:58 by 123bingbing
    答問題,做項(xiàng)目,賺積分,換大獎(jiǎng).
    我出錢你學(xué)習(xí),現(xiàn)在來www.mylinux.com.cn做趣味問答就能得到積分獎(jiǎng)勵(lì)并可兌換大獎(jiǎng)
    主站蜘蛛池模板: 韩国亚洲伊人久久综合影院| 大地资源中文在线观看免费版| 免费A级毛片无码无遮挡内射| 中文字幕亚洲一区二区va在线| 亚洲av无码av在线播放| 思思re热免费精品视频66| 亚洲ⅴ国产v天堂a无码二区| 一区二区三区免费精品视频| 拔擦拔擦8x华人免费久久| 亚洲一区二区三区不卡在线播放| 少妇太爽了在线观看免费视频 | 亚洲AV无码欧洲AV无码网站| 老司机福利在线免费观看| 日韩免费观看的一级毛片| 亚洲人成网站在线观看播放青青| 免费国产黄网站在线观看视频| 亚洲Av永久无码精品三区在线| 拍拍拍无挡免费视频网站| 中文字幕亚洲日本岛国片| 男女作爱免费网站| 免费国产成人高清在线观看麻豆| 亚洲一区二区三区丝袜| 在线观看免费黄色网址| 国产一区二区三区免费在线观看 | 特级做A爰片毛片免费69| 亚洲精品国产成人专区| 高清永久免费观看| 国产成人精品高清免费| 亚洲人成人77777在线播放| 久久青草免费91观看| 国产亚洲午夜高清国产拍精品| 老司机免费午夜精品视频| 免费观看毛片视频| 亚洲AV日韩综合一区尤物 | 久久久久久免费视频| 91亚洲自偷手机在线观看| 久久精品中文字幕免费| 国产亚洲av片在线观看16女人| 最近免费中文字幕中文高清| 国产亚洲情侣一区二区无码AV| 乱人伦中文视频在线观看免费|