---恢復內容開始---java
實驗十 泛型程序設計技術編程
實驗時間 2018-11-1數組
第一部分:理論知識總結微信
1.泛型也稱爲參數化類型,就是在定義類、方法、接口時,經過類型參數指示將要處理的對象類型。dom
.2.泛型程序設計:編寫代碼能夠被不少不一樣類型的對象所重用。post
.3.一個泛型類就是具備一個或多個類型變量的類,即建立用類型做爲參數的類。學習
.4.Pair類引入了一個類型變量T,用尖括號(<>)括起來,並放在類名的後面。泛型類能夠有多個類型變量。測試
.5.類定義中的類型變量用於指定方法的返回類型以及域、局部變量的類型。ui
.6.除了泛型類外,還能夠只單獨定義一個方法做爲泛型方法,用於指定方法參數或者返回值爲泛型類型,留待方法調用時肯定。7.泛型方法能夠聲明在泛型類中,也能夠聲明在普通類中。this
8.泛型變量上界:extends關鍵字所聲明的上界既能夠是一個類,也能夠是一個接口。
.9.一個類型變量或通配符能夠有多個限定,限定類型用「&」分割。
.10.泛型變量下界:經過使用super關鍵字能夠固定泛型參數的類型爲某種類型或者其超類。當程序但願爲一個方法的參數限定類型時,一般能夠使用下限通配符。
.11.Java中的數組是協變的。例如:Integer擴展了Number,那麼在要求Number[]的地方徹底能夠傳遞或者賦予Integer[],Number[]也是Integer[]的超類型。Employee 是Manager 的超類, 所以能夠將一個Manager[]數組賦給一個類型爲Employee[]的變量。
注:泛型類型的數組不是協變的。
1、實驗目的與要求
(1) 理解泛型概念;
(2) 掌握泛型類的定義與使用;
(3) 掌握泛型方法的聲明與使用;
(4) 掌握泛型接口的定義與實現;
(5)瞭解泛型程序設計,理解其用途。
2、實驗內容和步驟
實驗1: 導入第8章示例程序,測試程序並進行代碼註釋。
測試程序1:
l 編輯、調試、運行教材3十一、312頁 代碼,結合程序運行結果理解程序;
l 在泛型類定義及使用代碼處添加註釋;
l 掌握泛型類的定義及使 1 package pair1;
2 3 /** 4 * @version 1.01 2012-01-26 5 * @author Cay Horstmann 6 */ 7 public class PairTest1 8 { 9 public static void main(String[] args) 10 { 11 String[] words = { "Mary", "had", "a", "little", "lamb" };//初始化String對象數組 12 Pair<String> mm = ArrayAlg.minmax(words);//經過類名調用minmax方法 13 System.out.println("min = " + mm.getFirst()); 14 System.out.println("max = " + mm.getSecond()); 15 } 16 } 17 18 class ArrayAlg 19 { 20 /** 21 * Gets the minimum and maximum of an array of strings. 22 * @param a an array of strings 23 * @return a pair with the min and max value, or null if a is null or empty 24 */ 25 public static Pair<String> minmax(String[] a)//實例化的一個Pair類對象 26 { 27 if (a == null || a.length == 0) return null; 28 String min = a[0]; 29 String max = a[0]; 30 for (int i = 1; i < a.length; i++) 31 { 32 if (min.compareTo(a[i]) > 0) min = a[i];//字符串對象比較, 33 if (max.compareTo(a[i]) < 0) max = a[i]; 34 } 35 return new Pair<>(min, max);//泛型類做爲返回值 36 } 37 }
1 package pair1; 2 3 /** 4 * @version 1.00 2004-05-10 5 * @author Cay Horstmann 6 */ 7 public class Pair<T> 8 { 9 private T first; 10 private T second; 11 12 public Pair() { first = null; second = null; } 13 public Pair(T first, T second) { this.first = first; this.second = second; } 14 15 public T getFirst() { return first; } 16 public T getSecond() { return second; } 17 18 public void setFirst(T newValue) { first = newValue; } 19 public void setSecond(T newValue) { second = newValue; } 20 }
測試程序2:
l 編輯、調試運行教材315頁 PairTest2,結合程序運行結果理解程序;
l 在泛型程序設計代碼處添加相關注釋;
l 掌握泛型方法、泛型變量限定的定義及用途。
1 package pair2; 2 3 import java.time.*; 4 5 /** 6 * @version 1.02 2015-06-21 7 * @author Cay Horstmann 8 */ 9 public class PairTest2 10 { 11 public static void main(String[] args) 12 { 13 //初始化LocalDate對象數組 14 LocalDate[] birthdays = 15 { 16 LocalDate.of(1906, 12, 9), // G. Hopper 17 LocalDate.of(1815, 12, 10), // A. Lovelace 18 LocalDate.of(1903, 12, 3), // J. von Neumann 19 LocalDate.of(1910, 6, 22), // K. Zuse 20 }; 21 Pair<LocalDate> mm = ArrayAlg.minmax(birthdays);//經過類名調用minmax方法 22 System.out.println("min = " + mm.getFirst()); 23 System.out.println("max = " + mm.getSecond()); 24 } 25 } 26 27 class ArrayAlg 28 { 29 /** 30 Gets the minimum and maximum of an array of objects of type T. 31 @param a an array of objects of type T 32 @return a pair with the min and max value, or null if a is 33 null or empty 34 */ 35 public static <T extends Comparable> Pair<T> minmax(T[] a)//經過extends關鍵字增長上界約束的泛型方法 36 { 37 if (a == null || a.length == 0) return null; 38 T min = a[0]; 39 T max = a[0]; 40 for (int i = 1; i < a.length; i++) 41 { 42 if (min.compareTo(a[i]) > 0) min = a[i]; 43 if (max.compareTo(a[i]) < 0) max = a[i]; 44 } 45 return new Pair<>(min, max);//範型類做爲返回值 46 } 47 }
1 package pair2; 2 3 /** 4 * @version 1.00 2004-05-10 5 * @author Cay Horstmann 6 */ 7 public class Pair<T> 8 { 9 private T first; 10 private T second; 11 12 public Pair() { first = null; second = null; } 13 public Pair(T first, T second) { this.first = first; this.second = second; } 14 15 public T getFirst() { return first; } 16 public T getSecond() { return second; } 17 18 public void setFirst(T newValue) { first = newValue; } 19 public void setSecond(T newValue) { second = newValue; } 20 }
測試程序3:
l 用調試運行教材335頁 PairTest3,結合程序運行結果理解程序;
l 瞭解通配符類型的定義及用途。
1 public class PairTest3 2 { 3 public static void main(String[] args) 4 { 5 Manager ceo = new Manager("Gus Greedy", 800000, 2003, 12, 15); 6 Manager cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15); 7 Pair<Manager> buddies = new Pair<>(ceo, cfo); 8 printBuddies(buddies); 9 10 ceo.setBonus(1000000); 11 cfo.setBonus(500000); 12 Manager[] managers = { ceo, cfo }; 13 14 Pair<Employee> result = new Pair<>(); 15 16 17 minmaxBonus(managers, result); 18 System.out.println("first: " + result.getFirst().getName() 19 + ", second: " + result.getSecond().getName()); 20 maxminBonus(managers, result); 21 System.out.println("first: " + result.getFirst().getName() 22 + ", second: " + result.getSecond().getName()); 23 } 24 25 public static void printBuddies(Pair<? extends Employee> p)//通配符類型(帶有上界)extends關鍵字所聲明的上界既能夠是一個類,也能夠是一個接口。 26 { 27 Employee first = p.getFirst(); 28 Employee second = p.getSecond(); 29 System.out.println(first.getName() + " and " + second.getName() + " are buddies."); 30 } 31 32 public static void minmaxBonus(Manager[] a, Pair<? super Manager> result)//通配符類型(帶有下界)必須是Manager的子類 33 { 34 if (a.length == 0) return; 35 Manager min = a[0]; 36 Manager max = a[0]; 37 for (int i = 1; i < a.length; i++) 38 { 39 if (min.getBonus() > a[i].getBonus()) min = a[i]; 40 if (max.getBonus() < a[i].getBonus()) max = a[i]; 41 }//比較大小值 42 result.setFirst(min); 43 result.setSecond(max); 44 } 45 46 public static void maxminBonus(Manager[] a, Pair<? super Manager> result)//通配符類型(帶有下界) 47 { 48 minmaxBonus(a, result); 49 PairAlg.swapHelper(result); //swapHelper捕獲通配符類型 50 } 51 //沒法編寫公共靜態< T超級管理器> 52 } 53 54 class PairAlg 55 { 56 public static boolean hasNulls(Pair<?> p)//經過將hasNulls轉換成泛型方法,避免使用通配符類型 57 { 58 return p.getFirst() == null || p.getSecond() == null; 59 } 60 61 public static void swap(Pair<?> p) { swapHelper(p); } 62 63 public static <T> void swapHelper(Pair<T> p)//使用輔助方法swapHelper(泛型方法),以在交換時臨時保存第一個元素 64 { 65 T t = p.getFirst(); 66 p.setFirst(p.getSecond()); 67 p.setSecond(t); 68 }
1 public class Pair<T> 2 { 3 private T first; 4 private T second; 5 //T是未知類型,不表明值 6 public Pair() { first = null; second = null; } 7 public Pair(T first, T second) { this.first = first; this.second = second; } 8 9 public T getFirst() { return first; } 10 public T getSecond() { return second; } 11 12 public void setFirst(T newValue) { first = newValue; } 13 public void setSecond(T newValue) { second = newValue; } 14 }
1 import java.time.*; 2 3 public class Employee//用戶自定義類 4 { 5 private String name; 6 private double salary; 7 private LocalDate hireDay; 8 9 public Employee(String name, double salary, int year, int month, int day) 10 { 11 this.name = name; 12 this.salary = salary; 13 hireDay = LocalDate.of(year, month, day); 14 } 15 16 public String getName() 17 { 18 return name; 19 } 20 21 public double getSalary() 22 { 23 return salary; 24 } 25 26 public LocalDate getHireDay() 27 { 28 return hireDay; 29 } 30 31 public void raiseSalary(double byPercent) 32 { 33 double raise = salary * byPercent / 100; 34 salary += raise; 35 } 36 }
1 public class Manager extends Employee//繼承類 2 { 3 private double bonus; 4 5 /** 6 @param name the employee's name 7 @param salary the salary 8 @param year the hire year 9 @param month the hire month 10 @param day the hire day 11 */ 12 public Manager(String name, double salary, int year, int month, int day) 13 { 14 super(name, salary, year, month, day); 15 bonus = 0; 16 } 17 18 public double getSalary() 19 { 20 double baseSalary = super.getSalary(); 21 return baseSalary + bonus; 22 } 23 24 public void setBonus(double b) 25 { 26 bonus = b; 27 } 28 29 public double getBonus() 30 { 31 return bonus; 32 } 33 }
實驗2:編程練習:
編程練習1:實驗九編程題總結
l 實驗九編程練習1總結(從程序整體結構說明、模塊說明,目前程序設計存在的困難與問題三個方面闡述)。
:(1)該程序有兩個類構成,一個主類,
一個student類,該類實現了一個接口。
1 package text8; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 9 import java.util.ArrayList; 10 import java.util.Arrays; 11 import java.util.Collections; 12 import java.util.List; 13 import java.util.Scanner; 14 15 public class Xinxi { 16 private static ArrayList<Student> studentlist; 17 18 19 public static void main(String[] args) { 20 studentlist = new ArrayList<>(); 21 Scanner scanner = new Scanner(System.in); 22 File file = new File("D:\\身份證號\\身份證號.txt"); 23 try { 24 FileInputStream fis = new FileInputStream(file); 25 BufferedReader in = new BufferedReader(new InputStreamReader(fis)); 26 String temp = null; 27 while ((temp = in.readLine()) != null) { 28 29 Scanner linescanner = new Scanner(temp); 30 31 linescanner.useDelimiter(" "); 32 String name = linescanner.next(); 33 String number = linescanner.next(); 34 String sex = linescanner.next(); 35 String age = linescanner.next(); 36 String province = linescanner.nextLine(); 37 Student student = new Student(); 38 student.setName(name); 39 student.setnumber(number); 40 student.setsex(sex); 41 int a = Integer.parseInt(age); 42 student.setage(a); 43 student.setprovince(province); 44 studentlist.add(student); 45 46 } 47 } catch (FileNotFoundException e) {//添加的異常處理語句try{ }catch{ }語句 48 System.out.println("所找信息文件找不到"); 49 e.printStackTrace(); 50 } catch (IOException e) { 51 System.out.println("所找信息文件讀取錯誤");//採起積極方法捕獲異常,並將異常返回本身所設定的打印文字 52 e.printStackTrace(); 53 } 54 boolean isTrue = true; 55 while (isTrue) { 56 System.out.println("選擇你的操做,輸入正確格式的選項"); 57 System.out.println("1按姓名字典序輸出人員信息"); 58 System.out.println("2.查詢最大和最小年齡的人員信息"); 59 60 System.out.println("3.尋找老鄉"); 61 System.out.println("4.尋找年齡相近的人的信息"); 62 63 System.out.println("5.退出"); 64 String n = scanner.next(); 65 switch (n) { 66 case "1": 67 Collections.sort(studentlist); 68 System.out.println(studentlist.toString()); 69 break; 70 case "2": 71 int max = 0, min = 100; 72 int j, k1 = 0, k2 = 0; 73 for (int i = 1; i < studentlist.size(); i++) { 74 j = studentlist.get(i).getage(); 75 if (j > max) { 76 max = j; 77 k1 = i; 78 } 79 if (j < min) { 80 min = j; 81 k2 = i; 82 } 83 84 } 85 System.out.println("年齡最大:" + studentlist.get(k1)); 86 87 System.out.println("年齡最小:" + studentlist.get(k2)); 88 break; 89 case "3": 90 System.out.println("家鄉在哪裏?"); 91 String find = scanner.next(); 92 String place = find.substring(0, 3); 93 for (int i = 0; i < studentlist.size(); i++) { 94 if (studentlist.get(i).getprovince().substring(1, 4).equals(place)) 95 System.out.println("同鄉" + studentlist.get(i)); 96 } 97 break; 98 99 case "4": 100 System.out.println("年齡:"); 101 int yourage = scanner.nextInt(); 102 int near = agenear(yourage); 103 int value = yourage - studentlist.get(near).getage(); 104 System.out.println("" + studentlist.get(near)); 105 break; 106 case "5": 107 isTrue = false; 108 System.out.println("退出程序!"); 109 break; 110 default: 111 System.out.println("輸入有誤"); 112 113 } 114 } 115 } 116 117 public static int agenear(int age) { 118 int j = 0, min = 53, value = 0, flag = 0; 119 for (int i = 0; i < studentlist.size(); i++) { 120 value = studentlist.get(i).getage() - age; 121 if (value < 0) 122 value = -value; 123 if (value < min) { 124 min = value; 125 flag = i; 126 } 127 } 128 return flag; 129 } 130 131 }
1 package text8; 2 3 public class Student implements Comparable<Student> { 4 5 private String name; 6 private String number; 7 private String sex; 8 private String province; 9 private int age; 10 11 public void setName(String name) { 12 // TODO 自動生成的方法存根 13 this.name = name; 14 15 } 16 17 public String getName() { 18 // TODO 自動生成的方法存根 19 return name; 20 } 21 22 public void setnumber(String number) { 23 // TODO 自動生成的方法存根 24 this.number = number; 25 } 26 27 public String getNumber() { 28 // TODO 自動生成的方法存根 29 return number; 30 } 31 32 public void setsex(String sex) { 33 // TODO 自動生成的方法存根 34 this.sex = sex; 35 } 36 37 public String getsex() { 38 // TODO 自動生成的方法存根 39 return sex; 40 } 41 42 public void setprovince(String province) { 43 // TODO 自動生成的方法存根 44 this.province = province; 45 } 46 47 public String getprovince() { 48 // TODO 自動生成的方法存根 49 return province; 50 } 51 52 public void setage(int a) { 53 // TODO 自動生成的方法存根 54 this.age = age; 55 } 56 57 public int getage() { 58 // TODO 自動生成的方法存根 59 return age; 60 } 61 62 public int compareTo(Student o) { 63 return this.name.compareTo(o.getName()); 64 } 65 66 public String toString() { 67 return name + "\t" + sex + "\t" + age + "\t" + number + "\t" + province + "\n"; 68 } 69 }
(2)在該程序中添加異常處理語句。採用積極拋出異常的模式,處理程序中可能會出現的異常。
(3)目前程序設計存在的困難是,能夠將一個模塊一個模塊的寫出來,但不知道該怎麼將這些模塊組織起來成一個完整的程序。
還有在程序運行結果出現一些不應出現的符號時,不知道該如何準確的修改。
l 實驗九編程練習2總結(從程序整體結構說明、模塊說明,目前程序設計存在的困難與問題三個方面闡述)。
總結:(1)該程序有兩個類構成,一個主類,
一個用戶自定義類
2 import java.util.Random; 3 import java.util.Scanner; 4 import java.io.FileNotFoundException; 5 import java.io.PrintWriter; 6 7 public class Demo { 8 public static void main(String[] args) { 9 // 用戶的答案要從鍵盤輸入,所以須要一個鍵盤輸入流 10 //Scanner in = new Scanner(System.in); 11 yunsuan counter = new yunsuan (); 12 PrintWriter out = null; 13 14 try { 15 out = new PrintWriter("D:\\text.txt"); 16 } catch (FileNotFoundException e) { 17 // TODO Auto-generated catch block 18 e.printStackTrace(); 19 } 20 int sum = 0; 21 // 經過循環生成10道題 22 for (int i = 0; i < 10; i++) { 23 24 25 int a = (int) Math.round(Math.random() * 10); 26 int b = (int) Math.round(Math.random() * 10); 27 28 Scanner in1 =new Scanner(System.in); 29 30 31 switch((int)(Math.random()*4)) 32 33 { 34 35 case 1: 36 System.out.println( ""+a+"+"+b+"="); 37 38 int c1 = in1.nextInt(); 39 out.println(a+"+"+b+"="+c1); 40 if (c1 == counter.add(a, b)) { 41 sum += 10; 42 System.out.println("恭喜答案正確"); 43 } 44 else { 45 System.out.println("抱歉答案錯誤"); 46 } 47 48 break ; 49 case 2: 50 System.out.println(i + ": " + a + "-" + b + "="); 51 int c2 = in1.nextInt(); 52 out.println(a + "-" + b + "=" + c2); 53 if (c2 == counter.reduce(a, b)) { 54 sum += 10; 55 System.out.println("恭喜答案正確"); 56 } else { 57 System.out.println("抱歉答案錯誤"); 58 } 59 break; 60 case 3: 61 System.out.println(i + ": " + a + "*" + b + "="); 62 int c3 = in1.nextInt(); 63 out.println(a + "*" + b + "=" + c3); 64 if (c3 == counter.multiplication(a, b)) { 65 sum += 10; 66 System.out.println("恭喜答案正確"); 67 } else { 68 System.out.println("抱歉答案錯誤"); 69 } 70 break; 71 case 4: 72 System.out.println(""+a+"/"+b+"="); 73 while(b==0) 74 { b = (int) Math.round(Math.random() * 100); 75 } 76 int c4= in1.nextInt(); 77 out.println(a+"/"+b+"="+c4); 78 if (c4 == counter.devision(a, b)) { 79 sum += 10; 80 System.out.println("恭喜答案正確"); 81 } 82 else { 83 System.out.println("抱歉答案錯誤"); 84 } 85 break; 86 } 87 } 88 89 System.out.println("總分:"+sum); 90 out.println(sum); 91 92 out.close(); 93 } 94 } 95 1
2 3 public class yunsuan { 4 5 public int multiplication(int a, int b) { 6 // TODO 自動生成的方法存根 7 return a*b; 8 } 9 10 public int add(int a, int b) { 11 // TODO 自動生成的方法存根 12 return a+b; 13 } 14 15 public int reduce(int a, int b) { 16 // TODO 自動生成的方法存根 17 if((a-b)>0) 18 return a-b; 19 else 20 return 0; 21 } 22 23 public int devision(int a, int b) { 24 // TODO 自動生成的方法存根 25 if(b!=0) 26 return a/b; 27 else 28 return 0; 29 } 30 31 }
(2)存在的困難是,有些運算超出小學生的計算範圍,當回答正確時,會生成10道四則運算習題,當有一個回答不正確時,生成的四則運算習題不是十道。
將生成的十道四則運算題目保存在txt文檔中。
編程練習2:採用泛型程序設計技術改進實驗九編程練習2,使之可處理實數四則運算,其餘要求不變。
1 package PairTest1; 2 import java.io.*; 3 import java.util.*; 4 public class bbbb { 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 bbbb c = new bbbb(); 8 PrintWriter out = null; 9 try { 10 out = new PrintWriter("test.txt"); 11 } catch (FileNotFoundException e) { 12 System.out.println("輸出錯誤"); 13 e.printStackTrace(); 14 } 15 int sum = 0; 16 for (int i = 1; i <= 10; i++) { 17 int a = (int) Math.round(Math.random() * 100); 18 int b = (int) Math.round(Math.random() * 100); 19 int m ; 20 Random rand = new Random(); 21 m = (int) rand.nextInt(4) + 1; 22 switch (m) { 23 case 1: 24 //a = b + (int) Math.round(Math.random()* 100); 25 while(b == 0){ 26 b = (int) Math.round(Math.random()*100); 27 } 28 while(a % b != 0){ 29 a = (int) Math.round(Math.random() * 100); 30 } 31 System.out.println("i + " " + a + "/" + b + "="); 32 int c0 = in.nextInt(); 33 out.println(a + "/" + b + "=" + c0); 34 if (c0 == c.A(a,b)) { 35 sum += 10; 36 System.out.println("恭喜答案正確!"); 37 } else { 38 System.out.println("抱歉答案錯誤!"); 39 } 40 break; 41 case 2: 42 System.out.println("i+ " " + a + "*" + b + "="); 43 int yy= in.nextInt(); 44 out.println(a + "*" + b + "=" + c); 45 if (yy== c.B(a,b)) { 46 sum += 10; 47 System.out.println("恭喜答案正確!"); 48 } else { 49 System.out.println(「抱歉答案錯誤!"); 50 } 51 break; 52 case 3: 53 System.out.println(i + " " + a + "+" + b + "="); 54 int c1 = in.nextInt(); 55 out.println(a + "+" + b + "=" + c1); 56 if (c1 == c.C(a, b)) { 57 sum += 10; 58 System.out.println("恭喜答案正確!"); 59 } else { 60 System.out.println("抱歉答案錯誤!"); 61 } 62 break; 63 case 4: 64 while (a < b) { 65 b = (int) Math.round(Math.random() * 100); 66 } 67 System.out.println(i + " " + a + "-" + b + "="); 68 int c2 = in.nextInt(); 69 out.println(a + "-" + b + "=" + c2); 70 if (c2 == c.D(a, b)) { 71 sum += 10; 72 System.out.println("恭喜答案正確!"); 73 } else { 74 System.out.println("抱歉答案錯誤!"); 75 } 76 break; 77 } 78 } 79 System.out.println("總分" + sum); 80 out.println("總分" + sum); 81 out.close(); 82 } 83 84 private int D(int a, int b) { 85 // TODO Auto-generated method stub 86 return 0; 87 } 88 89 private int C(int a, int b) { 90 // TODO Auto-generated method stub 91 return 0; 92 } 93 94 private int B(int a, int b) { 95 // TODO Auto-generated method stub 96 return 0; 97 } 98 99 private int A(int a, int b) { 100 // TODO Auto-generated method stub 101 return 0; 102 } 103 104 105 }
1 package PairTest1; 2 3 public class hhhh { 4 5 public class AAAA<T> { 6 private T a1; 7 private T b1; 8 9 public AAAA() { 10 a1 = null; 11 b1 = null; 12 } 13 public AAAA(T a, T b) { 14 this.a1 = a; 15 this.b1 = b; 16 } 17 18 public int C(int a,int b) { 19 return a + b; 20 } 21 22 public int D(int a, int b) { 23 return a - b; 24 } 25 26 public int B(int a, int b) { 27 return a * b; 28 } 29 30 public int A(int a, int b) { 31 if (b != 0 && a%b==0) 32 return a / b; 33 else 34 return 0; 35 } 36 } 37 }
實驗總結:
1 泛型的概念定義:
i,引入了參數化類型(Parameterized Type)的概念,改造了全部的Java集合,使之都實現泛型,容許程序在建立集合時就能夠指定集合元素的類型,好比List<String>就表名這是一個只能存放String類型的List;
ii. 泛型(Generic):就是指參數化類型,上面的List<String>就是參數化類型,所以就是泛型,而String就是該List<String>泛型的類型參數;
3) 泛型的好處:
i. 使集合能夠記住元素類型,即取出元素的時候無需進行強制類型轉化了,能夠直接用原類型的引用接收;
ii. 一旦指定了性參數那麼集合中元素的類型就肯定了,不能添加其餘類型的元素,不然會直接編譯保存,這就能夠避免了「不當心放入其餘類型元素」的可能;
2,通配符
1.)在實例化對象的時候,不肯定泛型參數的具體類型時,能夠使用通配符進行對象定義。
2)<? extends Object>表明上邊界限定通配符
3) <? super Object>表明下邊界限定通配符。
感覺:
經過本週的學習,掌握了泛型類的定義,以及泛型方法的聲明,還有泛型接口的定義,以及對泛型變量的限定。但在用泛型類寫程序時仍是有一點點的困難。
在本身編程時,仍是有很大問題。在以後的學習中,我會多練習程序去了解這些知識,爭取可以獨立完整的編寫程序。