posts - 7, comments - 17, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

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 中的新特性。一种泛类型用一个或多个泛型变量定义,可以有一个或多个,泛型变量做参数占位符或做返回值的方法。例如,类型 java.util.List<E> 是一种泛类型:一个 list ,它的元素类型是 E 这个占位符表示的类型。这个类型有一个叫 add() 的方法,有一个参数类型为 E ,有一个名叫 get() 的方法,返回一个类型为 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.

为了可以象往常一样使用泛类型,你需要为泛型变量(或是变量)指定一个实际的类型,产生一个参数化类型,就象 List<String> 。这样做的原因是,为编译器提供一个特定的类型信息,让它可以在编译期为你做类型检查,这样可以大大增加你程序的类型安全。例如,有一个 List 打算容纳 String 类型的对象,这种类型安全检查阻止你增加一个 String[] 的元素。而且,这附加的类型信息使编译器帮你做些造型的活儿。例如,编译器知道 List<String> get() 方法返回一个 String 类型对象,你不再需要把 Object 类型的返回值造型成一个 String 类型的对象。

[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] 整个这章,我们将统一使用这些术语。 " 泛类型 " :意味着一个类型,可以声明一个或多个泛型变量。 " 参数化类型 " ,意味着一个(运行期的)泛类型,表示它的泛型变量被实际类型做为参数值替换了。但在通常的应用中,两个术语的差别不是太明显,有时还可以替换。

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 类都已经被泛化了,你可能会在程序中频繁的用到它们。类型安全的 Collection 是应用泛类型的典范。可能你还从没有定义过自己的泛类型,也从没用过 Collection (在包 java.uitl 中)之外的泛类型,但类型安全的 Collection 的好处是显而易见的,它将证实这个新的,复杂的,主要且重要的语言特性。

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 的基本泛化用法开始,然后深入泛类型用法更为复杂的细节。接下来,我们将覆盖泛型参数通配符和边界通配符。再说明怎么用泛类型,我们将阐述怎样写自己的泛类型和泛型方法。我们涉及的泛型知识包括了 Java API 中关于泛类型的主要部分。深入的探讨了这些类型和他们的用法,为的是更深入的理解泛型是如何工作的。

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-- 关于对象、 map-- 关于关键字对象和值对象的对 ) Collection 将在第五章讨论。这里我们只讨论有关在 Java 5.0 Collection 类中,用泛型参数标识 Collection 对象类型的内容。这在 Java 1.4 或是更早的 Java 版本中,没有这个用法。在没有泛型应用 Collection 的时候,需要程序员正确记得每个 Collection 的每个元素的类型。当我们建立一个 Java 1.4 Collection 时,你知道打算把什么样的类型对象放入这个 Collection 中,但是编译器不知道这些。你增加元素时必须小心正确性。并且当获取一个 Collection 的元素时,你必须显示的造型对象 Object 到它们实际的类型。思考在 Java 1.4 中的如下代码: 
   
太郁闷了,代码总是对不齐,算了,完整的还是发个pdf吧,要看自己下了,
不要忘了拍砖就行了!
pdf修订了一下




评论

# re: 自己翻译的Java.In.A.Nutshell.5th中泛型一章,欢迎拍砖把文章砸的漂亮一些  回复  更多评论   

2006-09-21 21:06 by 123bingbing
老兄,有空到[url]www.mylinux.com.cn[/url]坐坐。那里挺专业的。

# re: 自己翻译的Java.In.A.Nutshell.5th中泛型一章,欢迎拍砖把文章砸的漂亮一些  回复  更多评论   

2006-09-24 21:58 by 123bingbing
答问题,做项目,赚积分,换大奖.
我出钱你学习,现在来www.mylinux.com.cn做趣味问答就能得到积分奖励并可兑换大奖

只有注册用户登录后才能发表评论。


网站导航: