• /  25
  • 下載費用: 9.90積分  

Java語言程序設計與數據結構 基礎篇原書第11版PPT及其他教輔資源19slide.ppt

'Java語言程序設計與數據結構 基礎篇原書第11版PPT及其他教輔資源19slide.ppt'
*Chapter 19 Generics*ObjectivesTo know the benefits of generics (§19.1).To use generic classes and interfaces (§19.2).To declare generic classes and interfaces (§19.3).To understand why generic types can improve reliability and readability (§19.3).To declare and use generic methods and bounded generic types (§19.4).To use raw types for backward compatibility (§19.5).To know wildcard types and understand why they are necessary (§19.6).To convert legacy code using JDK 1.5 generics (§19.7).To understand that generic type information is erased by the compiler and all instances of a generic class share the same runtime class file (§19.8).To know certain restrictions on generic types caused by type erasure (§19.8).To design and implement generic matrix classes (§19.9).*Why Do You Get a Warning?public class ShowUncheckedWarning { public static void main(String[] args) { java.util.ArrayList list = new java.util.ArrayList(); list.add("Java Programming"); }}To understand the compile warning on this line, you need to learn JDK 1.6 generics.*Fix the Warningpublic class ShowUncheckedWarning { public static void main(String[] args) { java.util.ArrayList list = new java.util.ArrayList(); list.add("Java Programming"); }}No compile warning on this line.`*What is Generics? Generics is the capability to parameterize types. With this capability, you can define a class or a method with generic types that can be substituted using concrete types by the compiler. For example, you may define a generic stack class that stores the elements of a generic type. From this generic class, you may create a stack object for holding strings and a stack object for holding numbers. Here, strings and numbers are concrete types that replace the generic type.*Why Generics? The key benefit of generics is to enable errors to be detected at compile time rather than at runtime. A generic class or method permits you to specify allowable types of objects that the class or method may work with. If you attempt to use the class or method with an incompatible object, a compile error occurs.*Generic TypeGeneric Instantiation Runtime errorCompile errorImproves reliability*Generic ArrayList in JDK 1.5*No Casting NeededArrayList list = new ArrayList();list.add(5.5); // 5.5 is automatically converted to new Double(5.5)list.add(3.0); // 3.0 is automatically converted to new Double(3.0)Double doubleObject = list.get(0); // No casting is neededdouble d = list.get(1); // Automatically converted to double*Declaring Generic Classes and Interfaces GenericStack*Generic Methods public static void print(E[] list) { for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); } public static void print(Object[] list) { for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); }*Bounded Generic Typepublic static void main(String[] 。省略部分。c static boolean equalArea(E object1, E object2) { return object1.getArea() == object2.getArea();}*Raw Type and Backward Compatibility // raw typeArrayList list = new ArrayList(); This is roughly equivalent to ArrayList list = new ArrayList(); *Raw Type is Unsafe Max.max("Welcome", 23); // Max.java: Find a maximum objectpublic class Max { /** Return the maximum between two objects */ public static Comparable max(Comparable o1, Comparable o2) { if (o1.compareTo(o2) > 0) return o1; else return o2; }}Runtime Error: *Make it Safe Max.max("Welcome", 23); // Max1.java: Find a maximum objectpublic class Max1 { /** Return the maximum between two objects */ public static <E extends Comparable> E max(E o1, E o2) { if (o1.compareTo(o2) > 0) return o1; else return o2; }}*Wildcards Why wildcards are necessary? See this example.? unbounded wildcard ? extends T bounded wildcard ? super T lower bound wildcard WildCardNeedDemoAnyWildCardDemoSuperWildCardDemo*Generic Types and Wildcard Types*Avoiding Unsafe Raw Types Use new ArrayList()Instead of new ArrayList();RunTestArrayListNew*Erasure and Restrictions on Generics Generics are implemented using an approach called type erasure. The compiler uses the generic type information to compile the code, but erases it afterwards. So the generic information is not available at run time. This approach enables the generic code to be backward-compatible with the legacy code that uses raw types.*Compile Time Checking For example, the compiler checks whether generics is used correctly for the following code in (a) and translates it into the equivalent code in (b) for runtime use. The code in (b) uses the raw type.*Important Facts It is important to note that a generic class is shared by all its instances regardless of its actual generic type. GenericStack stack1 = new GenericStack();GenericStack stack2 = new GenericStack();Although GenericStack and GenericStack are two types, but there is only one class GenericStack loaded into the JVM. *Restrictions on Generics Restriction 1: Cannot Create an Instance of a Generic Type. (i.e., new E()).Restriction 2: Generic Array Creation is Not Allowed. (i.e., new E[100]).Restriction 3: A Generic Type Parameter of a Class Is Not Allowed in a Static Context.Restriction 4: Exception Classes Cannot be Generic.*Designing Generic Matrix ClassesObjective: This example gives a generic class for matrix arithmetic. This class implements matrix addition and multiplication common for all types of matrices.GenericMatrix*UML Diagram*Objective: This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic.Source CodeRunTestIntegerMatrixIntegerMatrixRunTestRationalMatrixRationalMatrix
關 鍵 詞:
基礎 ppt 11 及其他 結構 數據 資源 程序設計 19slide 語言
 天天文庫所有資源均是用戶自行上傳分享,僅供網友學習交流,未經上傳用戶書面授權,請勿作他用。
關于本文
本文標題:Java語言程序設計與數據結構 基礎篇原書第11版PPT及其他教輔資源19slide.ppt
鏈接地址: http://www.476824.live/p-51617619.html
關于我們 - 網站聲明 - 網站地圖 - 資源地圖 - 友情鏈接 - 網站客服點擊這里,給天天文庫發消息,QQ:1290478887 - 聯系我們

本站為“文檔C2C交易模式”,即用戶上傳的文檔直接賣給(下載)用戶,本站只是中間服務平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有【成交的100%(原創)】。本站是網絡服務平臺方,若您的權利被侵害,侵權客服QQ:1290478887 歡迎舉報。

[email protected] 2017-2027 http://www.476824.live 網站版權所有

粵ICP備19057495號 

收起
展開
球探网即时蓝球比分 秒速赛车什么方法稳 陕西十一选五 3,6,9,10,11 山东11选5真准网 陕西11选5杀号选任八 点点涨配资 广东36选7好彩3奖金 哪个时时彩平台注册送彩金 股票配资平台哪家好 欢乐彩软件下载 宝尚配资