201771010126 王燕《面向物件程式設計(Java)》第十週學習總結
實驗十 泛型程式設計技術
實驗時間 2018-11-1
1、實驗目的與要求
(1) 理解泛型概念;
泛型:也稱引數化型別(parameterized type),就是在定義類、介面和方法時,通過型別引數指示將要處理的物件型別。(如ArrayList類). 泛型程式設計(Generic programming):編寫程式碼可以被很多不同型別的物件所重用。
(2) 掌握泛型類的定義與使用;
一個泛型類(generic class)就是具有一個或多個型別變數的類,即建立用型別作為引數的類。如一個泛型類定義格式如下:
class Generics<K,V>其中的K和V是類中的可變型別引數。
(3) 掌握泛型方法的宣告與使用;
泛型方法
– 除了泛型類外,還可以只單獨定義一個方法作
為泛型方法,用於指定方法引數或者返回值為
泛型型別,留待方法呼叫時確定。
– 泛型方法可以宣告在泛型類中,也可以宣告在
普通類中。
(4) 掌握泛型介面的定義與實現;
定義:
public interface IPool <T>
{
T get();
int add(T t);
}
實現:
public class GenericPool<T> implements IPool<T>
{
…
}
>
}
public class GenericPool implements IPool<Account>
{
…
}
(5)瞭解泛型程式設計,理解其用途。
泛型程式設計小結
. 定義一個泛型類時,在“<>”內定義形式型別引數,例如:“class TestGeneric<K, V>”,其中“K” , “V”不代表值,而是表示型別。. 例項化泛型物件的時候,一定要在類名後面指定型別引數的值(型別),一共要有兩次書寫。例如:
TestGeneric<String, String> t
=new TestGeneric<String, String>();
. 泛型中<T extends Object>, extends並不代表繼承,它是類型範圍限制。
. 泛型類不是協變的。
2、實驗內容和步驟
實驗1: 匯入第8章示例程式,測試程式並進行程式碼註釋。
測試程式1:
l 編輯、除錯、執行教材311、312頁 程式碼,結合程式執行結果理解程式;
l 在泛型類定義及使用程式碼處添加註釋;
l 掌握泛型類的定義及使用。
1 public class pair<T>//T是型別變數或引數化變數
2 {
3 private T first;
4 private T second;
5 public pair()
6 {
7 first = null; second = null;
8 }
9 public pair(T first, T second)
10 {
11 this.first = first;
12 this.second = second;
13 }
14 public T getFirst()
15 {
16 return first;
17 }
18 public T getSecond()
19 {
20 return second;
21 }
22 public void setFirst(T newValue)
23 {
24 first = newValue;
25 }
26 public void setSecond(T newValue)
27 {
28 second = newValue;
29 }
30 }
1 package Pari1;
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 }
1 package Pari1;
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" };
12 Pair<String> mm = ArrayAlg.minmax(words);
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)
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 }
測試程式2:
l 編輯、除錯執行教材315頁 PairTest2,結合程式執行結果理解程式;
l 在泛型程式設計程式碼處新增相關注釋;
l 掌握泛型方法、泛型變數限定的定義及用途。
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 }
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[] birthdays =
14 {
15 LocalDate.of(1906, 12, 9), // G. Hopper
16 LocalDate.of(1815, 12, 10), // A. Lovelace
17 LocalDate.of(1903, 12, 3), // J. von Neumann
18 LocalDate.of(1910, 6, 22), // K. Zuse
19 };
20 Pair<LocalDate> mm = ArrayAlg.minmax(birthdays);
21 System.out.println("min = " + mm.getFirst());
22 System.out.println("max = " + mm.getSecond());
23 }
24 }
25
26 class ArrayAlg
27 {
28 /**
29 Gets the minimum and maximum of an array of objects of type T.
30 @param a an array of objects of type T
31 @return a pair with the min and max value, or null if a is
32 null or empty
33 */
34 public static <T extends Comparable> Pair<T> minmax(T[] a)
35 {
36 if (a == null || a.length == 0) return null;
37 T min = a[0];
38 T max = a[0];
39 for (int i = 1; i < a.length; i++)
40 {
41 if (min.compareTo(a[i]) > 0) min = a[i];
42 if (max.compareTo(a[i]) < 0) max = a[i];
43 }
44 return new Pair<>(min, max);
45 }
46 }
測試程式3:
l 用除錯執行教材335頁 PairTest3,結合程式執行結果理解程式;
l 瞭解萬用字元型別的定義及用途。
1 package pair3;
2
3 import java.time.*;
4
5 public class Employee
6 {
7 private String name;
8 private double salary;
9 private LocalDate hireDay;
10
11 public Employee(String name, double salary, int year, int month, int day)
12 {
13 this.name = name;
14 this.salary = salary;
15 hireDay = LocalDate.of(year, month, day);
16 }
17
18 public String getName()
19 {
20 return name;
21 }
22
23 public double getSalary()
24 {
25 return salary;
26 }
27
28 public LocalDate getHireDay()
29 {
30 return hireDay;
31 }
32
33 public void raiseSalary(double byPercent)
34 {
35 double raise = salary * byPercent / 100;
36 salary += raise;
37 }
38 }
1 package pair3;
2
3 public class Manager extends Employee
4 {
5 private double bonus;
6
7 /**
8 @param name the employee's name
9 @param salary the salary
10 @param year the hire year
11 @param month the hire month
12 @param day the hire day
13 */
14 public Manager(String name, double salary, int year, int month, int day)
15 {
16 super(name, salary, year, month, day);
17 bonus = 0;
18 }
19
20 public double getSalary()
21 {
22 double baseSalary = super.getSalary();
23 return baseSalary + bonus;
24 }
25
26 public void setBonus(double b)
27 {
28 bonus = b;
29 }
30
31 public double getBonus()
32 {
33 return bonus;
34 }
35 }
1 package pair3;
2
3 /**
4 * @version 1.00 2004-05-10
5 * @author Cay Horstmann
6 */
7 public class Pair<T> //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 }
1 package pair3;
2
3 /**
4 * @version 1.01 2012-01-26
5 * @author Cay Horstmann
6 */
7 public class PairTest3
8 {
9 public static void main(String[] args)
10 {
11 Manager ceo = new Manager("Gus Greedy", 800000, 2003, 12, 15);
12 Manager cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15);
13 Pair<Manager> buddies = new Pair<>(ceo, cfo);
14 printBuddies(buddies);
15
16 ceo.setBonus(1000000);
17 cfo.setBonus(500000);
18 Manager[] managers = { ceo, cfo };
19
20 Pair<Employee> result = new Pair<>();
21 minmaxBonus(managers, result);
22 System.out.println("first: " + result.getFirst().getName()
23 + ", second: " + result.getSecond().getName());
24 maxminBonus(managers, result);
25 System.out.println("first: " + result.getFirst().getName()
26 + ", second: " + result.getSecond().getName());
27 }
28
29 public static void printBuddies(Pair<? extends Employee> p)
30 {
31 Employee first = p.getFirst();
32 Employee second = p.getSecond();
33 System.out.println(first.getName() + " and " + second.getName() + " are buddies.");
34 }
35
36 public static void minmaxBonus(Manager[] a, Pair<? super Manager> result)
37 {
38 if (a.length == 0) return;
39 Manager min = a[0];
40 Manager max = a[0];
41 for (int i = 1; i < a.length; i++)
42 {
43 if (min.getBonus() > a[i].getBonus()) min = a[i];
44 if (max.getBonus() < a[i].getBonus()) max = a[i];
45 }
46 result.setFirst(min);
47 result.setSecond(max);
48 }
49
50 public static void maxminBonus(Manager[] a, Pair<? super Manager> result)
51 {
52 minmaxBonus(a, result);
53 PairAlg.swapHelper(result); // OK--swapHelper captures wildcard type
54 }
55 // Can't write public static <T super manager> ...
56 }
57
58 class PairAlg
59 {
60 public static boolean hasNulls(Pair<?> p)/*(?)型別變數的萬用字元,“?”符號表明引數的型別可以是任何一種類
型,它和引數T的含義是有區別的。T表示一種 未知型別,而“?”表示任何一種型別。這種萬用字元一般有以下三種用法:
– 單獨的?,用於表示任何型別。
– ? extends type,表示帶有上界。
– ? super type,表示帶有下界。*/
61 {
62 return p.getFirst() == null || p.getSecond() == null;
63 }
64
65 public static void swap(Pair<?> p) { swapHelper(p); }
66
67 public static <T> void swapHelper(Pair<T> p)
68 {
69 T t = p.getFirst();
70 p.setFirst(p.getSecond());
71 p.setSecond(t);
72 }
73 }
實驗2:程式設計練習:
程式設計練習1:實驗九程式設計題總結
l 實驗九程式設計練習1總結(從程式總體結構說明、模組說明,目前程式設計存在的困難與問題三個方面闡述)。
1 import java.io.BufferedReader;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.Scanner;
11
12 public class Check{
13 private static ArrayList<Student> studentlist;
14 public static void main(String[] args) {
15 studentlist = new ArrayList<>();
16 Scanner scanner = new Scanner(System.in);
17 File file = new File("G:\\JAVA\\實驗\\身份證號.txt");//匯入文字檔案
18 try {
19 FileInputStream fis = new FileInputStream(file);
20 BufferedReader in = new BufferedReader(new InputStreamReader(fis));
21 String temp = null;
22 while ((temp = in.readLine()) != null) {
23
24 Scanner linescanner = new Scanner(temp);//建立輸入流,與文字檔案中的資訊進行匹配
25
26 linescanner.useDelimiter(" ");
27 String name = linescanner.next();
28 String number = linescanner.next();
29 String sex = linescanner.next();
30 String age = linescanner.next();
31 String province =linescanner.nextLine();
32 Student student = new Student();
33 student.setName(name);
34 student.setnumber(number);
35 student.setsex(sex);
36 int a = Integer.parseInt(age);
37 student.setage(a);
38 student.setprovince(province);
39 studentlist.add(student);
40
41 }
42 } catch (FileNotFoundException e) {
43 System.out.println("學生資訊檔案找不到");
44 e.printStackTrace();
45 } catch (IOException e) {
46 System.out.println("學生資訊檔案讀取錯誤");
47 e.printStackTrace();
48 }
49 boolean isTrue = true;
50 while (isTrue) {
51 System.out.println("選擇你的操作,輸入正確格式的選項");//控制檯輸出使用者的選擇
52 System.out.println("1.按姓名字典序輸出人員資訊");
53 System.out.println("2.輸出年齡最大和年齡最小的人");
54 System.out.println("3.查詢老鄉");
55 System.out.println("4.查詢年齡相近的人");
56 System.out.println("5.退出");
57 String m = scanner.next();
58 switch (m) {//使用catch語句對使用者的選擇作不同的操作
59 case "1":
60 Collections.sort(studentlist);
61 System.out.println(studentlist.toString());
62 break;
63 case "2":
64 int max=0,min=100;
65 int j,k1 = 0,k2=0;
66 for(int i=1;i<studentlist.size();i++)
67 {
68 j=studentlist.get(i).getage();
69 if(j>max)
70 {
71 max=j;
72 k1=i;
73 }
74 if(j<min)
75 {
76 min=j;
77 k2=i;
78 }
79
80 }
81 System.out.println("年齡最大:"+studentlist.get(k1));
82 System.out.println("年齡最小:"+studentlist.get(k2));
83 break;
84 case "3":
85 System.out.println("輸入省份");
86 String find = scanner.next();
87 String place=find.substring(0,3);
88 for (int i = 0; i <studentlist.size(); i++)
89 {
90 if(studentlist.get(i).getprovince().substring(1,4).equals(place))
91 System.out.println("老鄉"+studentlist.get(i));
92 }
93 break;
94
95 case "4":
96 System.out.println("年齡:");
97 int yourage = scanner.nextInt();
98 int near=agenear(yourage);
99 int value=yourage-studentlist.get(near).getage();
100 System.out.println(""+studentlist.get(near));
101 break;
102 case "5":
103 isTrue = false;
104 System.out.println("退出程式!");
105 break;
106 default:
107 System.out.println("輸入有誤");
108
109 }
110 }
111 }
112 public static int agenear(int age) {
113 int j=0,min=53,value=0,k=0;
114 for (int i = 0; i < studentlist.size(); i++)
115 {
116 value=studentlist.get(i).getage()-age;
117 if(value<0) value=-value;
118 if (value<min)
119 {
120 min=value;
121 k=i;
122 }
123 }
124 return k;
125 }
126
127 }
1 public class Student implements Comparable<Student> {
2
3 private String name;
4 private String number ;
5 private String sex ;
6 private int age;
7 private String province;
8
9 public String getName() {//建立主類所需的變數所使用的一系列方法
10 return name;
11 }
12 public void setName(String name) {
13 this.name = name;
14 }
15 public String getnumber() {
16 return number;
17 }
18 public void setnumber(String number) {
19 this.number = number;
20 }
21 public String getsex() {
22 return sex ;
23 }
24 public void setsex(String sex ) {
25 this.sex =sex ;
26 }
27 public int getage() {
28
29 return age;
30 }
31 public void setage(int age) {
32 // int a = Integer.parseInt(age);
33 this.age= age;
34 }
35
36 public String getprovince() {
37 return province;
38 }
39 public void setprovince(String province) {
40 this.province=province ;
41 }
42
43 public int compareTo(Student o) {
44 return this.name.compareTo(o.getName());
45 }
46
47 public String toString() {
48 return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n";
49 }
50 }
程式總體結結構:
Check類作為主類
Student類實現Comparable<Student>介面
模組說明:
主類Check類模組說明:1.匯入文字檔案;2.建立輸入流,與文字檔案中的資訊進行匹配;3.控制檯輸出使用者的選擇,並使用catch語句對使用者的選擇作不同的操作
Student類模組說明:建立主類所需的變數所使用的一系列方法
目前設計程式存在的困難與問題:匯入文字檔案失敗時的處理方式;由於C語言中沒有Boolean型別,程式中對Boolean型別在整個程式中的使用不夠熟練,常常使用較長的判斷語句而忽略了布林型別的運用
l 實驗九程式設計練習2總結(從程式總體結構說明、模組說明,目前程式設計存在的困難與問題三個方面闡述)。
1 import java.io.FileNotFoundException;
2 import java.io.PrintWriter;
3 import java.util.Scanner;
4 public class Caculator {
5 public static void main(String[] args) {
6 Scanner in = new Scanner(System.in);
7 Caculator1 computing=new Caculator1();
8 PrintWriter output = null;
9 try {
10 output = new PrintWriter("Caculator.txt");//將程式所出的十道題目以及使用者的作答以文字形式儲存在Caculator.txt中
11 } catch (Exception e) {
12 }
13 int sum = 0;
14
15 for (int i = 1; i < 11; i++) {
16 int a = (int) Math.round(Math.random() * 100);//在0到100以內隨機生成兩個數作為用算資料
17 int b = (int) Math.round(Math.random() * 100);//在0到3內隨機生成一個數作為運算子號的選擇符號
18 int s = (int) Math.round(Math.random() * 3);
19 switch(s)//在0到3內隨機生成一個數作為運算子號的選擇符號進行匹配相應的運算
20 {
21 case 1:
22 System.out.println(i+": "+a+"/"+b+"=");
23 while(b==0){
24 b = (int) Math.round(Math.random() * 100);
25 }
26 double c = in.nextDouble();
27 output.println(a+"/"+b+"="+c);
28 if (c == (double)computing.division(a, b)) {
29 sum += 10;
30 System.out.println("T");
31 }
32 else {
33 System.out.println("F");
34 }
35
36 break;
37
38 case 2:
39 System.out.println(i+": "+a+"*"+b+"=");
40 int c1 = in.nextInt();
41 output.println(a+"*"+b+"="+c1);
42 if (c1 == computing.multiplication(a, b)) {
43 sum += 10;
44 System.out.println("T");
45 }
46 else {
47 System.out.println("F");
48 }
49 break;
50 case 3:
51 System.out.println(i+": "+a+"+"+b+"=");
52 int c2 = in.nextInt();
53 output.println(a+"+"+b+"="+c2);
54 if (c2 == computing.addition(a, b)) {
55 sum += 10;
56 System.out.println("T");
57 }
58 else {
59 System.out.println("F");
60 }
61
62 break ;
63 case 4:
64 System.out.println(i+": "+a+"-"+b+"=");
65 int c3 = in.nextInt();
66 output.println(a+"-"+b+"="+c3);
67 if (c3 == computing.subtraction(a, b)) {
68 sum += 10;
69 System.out.println("T");
70 }
71 else {
72 System.out.println("F");
73 }
74 break ;
75
76 }
77
78 }
79 System.out.println("scores:"+sum);//每答對一道題相應的得到10分,最後將所有分數進行累加並且答應輸出
80 output.println("scores:"+sum);
81 output.close();
82
83 }
84 }
85 class Caculator1
86 {
87 private int a;//建立變數,
88 private int b;
89 public int addition(int a,int b)//定義四種運算方法
90 {
91 return a+b;
92 }
93 public int subtraction(int a,int b)
94 {
95 if((a-b)<0)
96 return 0;
97 else
98 return a-b;
99 }
100 public int multiplication(int a,int b)
101 {
102 return a*b;
103 }
104 public int division(int a,int b)
105 {
106 if(b!=0)
107 return a/b;
108 else
109 return 0;
110 }
111
112
113 }
程式總體結結構:
主類Caculator和Caculator1組成
模組說明:
主類Caculator:1.將程式所出的十道題目以及使用者的作答以文字形式儲存在Caculator.txt中;2.在0到100以內隨機生成兩個數作為用算資料,在0到3內隨機生成一個數作為運算子號的選擇符號;3.使用catch語句對0到3內隨機生成一個數作為運算子號的選擇符號進行匹配相應的運算;4.每答對一道題相應的得到10分,最後將所有分數進行累加並且答應輸出
Caculator1類:建立變數,並且定義四種運算方法
目前設計程式存在的困難與問題:將使用者輸入以及控制檯輸入儲存為文字形式的操作不熟練,沒有很好的掌握;以及用算中一些小學生涉及不到的運算知識,例如相減後結果為負號,以及除法運算後出現小數,甚至無線小數的情況沒有做具體的改進方法
程式設計練習2:採用泛型程式設計技術改進實驗九程式設計練習2,使之可處理實數四則運算,其他要求不變。
1 import java.io.FileNotFoundException;
2 import java.io.PrintWriter;
3 import java.util.Scanner;
4 public class Caculator {
5 public static void main(String[] args) {
6 Scanner in = new Scanner(System.in);
7 Caculator1 computing=new Caculator1();
8 PrintWriter output = null;
9 try {
10 output = new PrintWriter("Caculator.txt");//將程式所出的十道題目以及使用者的作答以文字形式儲存在Caculator.txt中
11 } catch (Exception e) {
12 }
13 int sum = 0;
14
15 for (int i = 1; i < 11; i++) {
16 int a = (int) Math.round(Math.random() * 100);//在0到100以內隨機生成兩個數作為用算資料
17 int b = (int) Math.round(Math.random() * 100);//在0到3內隨機生成一個數作為運算子號的選擇符號
18 int s = (int) Math.round(Math.random() * 3);
19 switch(s)//在0到3內隨機生成一個數作為運算子號的選擇符號進行匹配相應的運算
20 {
21 case 1:
22 System.out.println(i+": "+a+"/"+b+"=");
23 while(b==0){
24 b = (int) Math.round(Math.random() * 100);
25 }
26 double c = in.nextDouble();
27 output.println(a+"/"+b+"="+c);
28 if (c == (double)computing.division(a, b)) {
29 sum += 10;
30 System.out.println("T");
31 }
32 else {
33 System.out.println("F");
34 }
35
36 break;
37
38 case 2:
39 System.out.println(i+": "+a+"*"+b+"=");
40 int c1 = in.nextInt();
41 output.println(a+"*"+b+"="+c1);
42 if (c1 == computing.multiplication(a, b)) {
43 sum += 10;
44 System.out.println("T");
45 }
46 else {
47 System.out.println("F");
48 }
49 break;
50 case 3:
51 System.out.println(i+": "+a+"+"+b+"=");
52 int c2 = in.nextInt();
53 output.println(a+"+"+b+"="+c2);
54 if (c2 == computing.addition(a, b)) {
55 sum += 10;
56 System.out.println("T");
57 }
58 else {
59 System.out.println("F");
60 }
61
62 break ;
63 case 4:
64 System.out.println(i+": "+a+"-"+b+"=");
65 int c3 = in.nextInt();
66 output.println(a+"-"+b+"="+c3);
67 if (c3 == computing.subtraction(a, b)) {
68 sum += 10;
69 System.out.println("T");
70 }
71 else {
72 System.out.println("F");
73 }
74 break ;
75
76 }
77
78 }
79 System.out.println("scores:"+sum);//每答對一道題相應的得到10分,最後將所有分數進行累加並且答應輸出
80 output.println("scores:"+sum);
81 output.close();
82
83 }
84 }
85 class Caculator1
86 {
87 private int a;//建立變數,
88 private int b;
89 public int addition(int a,int b)//定義四種運算方法
90 {
91 return a+b;
92 }
93 public int subtraction(int a,int b)
94 {
95 if((a-b)<0)
96 return 0;
97 else
98 return a-b;
99 }
100 public int multiplication(int a,int b)
101 {
102 return a*b;
103 }
104 public int division(int a,int b)
105 {
106 if(b!=0)
107 return a/b;
108 else
109 return 0;
110 }
111
112
113 }
實驗總結:泛型程式主要應用於相似場景下不同的類以及物件,定義泛型類時用尖括號將引入的型別變數括起來跟在類名之後;泛型變數的上界的定義:public class NumberGeneric< T extends Number>:泛型變數的下界的定義:List<? super CashCard> cards = new ArrayList<T>();Java中的陣列是協變的,但泛型型別不滿足這一原理;“?”符號表明引數的型別可以是任何一種類
型,它和引數T的含義是有區別的。T表示一種
未知型別,而“?”表示任何一種型別。這種
萬用字元一般有以下三種用法:
– 單獨的?,用於表示任何型別。
– ? extends type,表示帶有上界。
– ? super type,表示帶有下界。
例項化泛型物件的時候,一定要在類名後面指定類
型引數的值(型別),一共要有兩次書寫。例如:
TestGeneric<String, String> t
=new TestGeneric<String, String>();
泛型中<T extends Object>, extends並不代表繼
承,它是類型範圍限制。