文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > 高等教育 > 工学 > Java语言程序设计基础篇全习题答案21
侵权投诉

Java语言程序设计基础篇全习题答案21

原书第6版第21章:范型

Chapter 21 Generics

1. (a) will compile fine, but (b) has a compilation error on Line 3, because dates is declared as a list of Date objects. You cannot assign a string to the list.

2. casting is needed in (a), but no casting is necessary in (b) with the generic type ArrayList<Date>.

3. One important benefit is improving reliability and robustness. Potential errors can be detected by the compiler.

4. No.

5. No.

6. Yes.

7. To declare a generic type for a class, place the generic type after the class name, such as GenericStack<E>. To declare a generic type for a method, place the generic type for the method return type, such as <E> void max(E o1, E o2).

8. Bounded generic type such as <E extends AClass> specifies that a generic type must be a subclass of AClass.

9. When you use generic type without specifying an actual parameter, it is called a raw type. GenericStack is roughly equivalent to GenericStack<Object>, but they are not type.

10. ? is unbounded wildcard

? extends T is bounded wildcard

? super T is lower bounded wildcard

11. No. Only ArrayList is loaded.

12. Since all instances of a generic class have the same runtime class, the static variables and methods of a

generic class is shared by all its instances. Therefore, it is illegal to refer a generic type parameter for a class in a static method or initializer.

第1页

你可能喜欢

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email