1. 程式人生 > >李瑞紅201771010111第十週 學習總結

李瑞紅201771010111第十週 學習總結

---恢復內容開始---

實驗十  泛型程式設計技術

實驗時間 2018-11-1

第一部分:理論知識總結

1.泛型也稱為引數化型別,就是在定義類、方法、介面時,通過型別引數指示將要處理的物件型別。

.2.泛型程式設計:編寫程式碼可以被很多不同型別的物件所重用。

.3.一個泛型類就是具有一個或多個型別變數的類,即建立用型別作為引數的類。

.4.Pair類引入了一個型別變數T,用尖括號(<>)括起來,並放在類名的後面。泛型類可以有多個型別變數。

.5.類定義中的型別變數用於指定方法的返回型別以及域、區域性變數的型別。

.6.除了泛型類外,還可以只單獨定義一個方法作為泛型方法,用於指定方法引數或者返回值為泛型型別,留待方法呼叫時確定。7.泛型方法可以宣告在泛型類中,也可以宣告在普通類中。

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:

編輯、除錯、執行教材311、312頁 程式碼,結合程式執行結果理解程式;

在泛型類定義及使用程式碼處添加註釋;

掌握泛型類的定義及使 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:

編輯、除錯執行教材315 PairTest2,結合程式執行結果理解程式;

在泛型程式設計程式碼處新增相關注釋;

掌握泛型方法、泛型變數限定的定義及用途。

 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:

用除錯執行教材335 PairTest3,結合程式執行結果理解程式;

瞭解萬用字元型別的定義及用途。

 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>代表下邊界限定萬用字元。

感受:

通過本週的學習,掌握了泛型類的定義,以及泛型方法的宣告,還有泛型介面的定義,以及對泛型變數的限定。但在用泛型類寫程式時還是有一點點的困難。

在自己程式設計時,還是有很大問題。在之後的學習中,我會多練習程式去了解這些知識,爭取能夠獨立完整的編寫程式。

      好文要頂  關注我  收藏該文