201871010111-刘佳华《面向对象程序设计(java)》第十一周学习总结

201871010111-刘佳华《面向对象程序设计(java)》第十一周学习总结

实验九  泛型程序设计技术

实验时间 2019-11-8

1、实验目的与要求

(1) 理解泛型概念;

(2) 掌握泛型类的定义与使用;

(3) 了解泛型方法的声明与使用;

(4) 掌握泛型接口的定义与实现;

(5) 理解泛型程序设计,理解其用途

第一部分:理论知识总结

1、泛型程序设计概念

  • JDK 5.0 中增加的泛型类型,是Java 语言中类型安全的一次重要改进。
  • 泛型:也称参数化类型(parameterized type),就是在定义类、接口和方法时,通过类型参数指示将要处理的对象类型。(如ArrayList类) 
  • 泛型程序设计(Generic programming):编写代码可以被很多不同类型的对象所重用。

2、泛型类的声明及实例化的方法

  • 一个泛型类(generic class)就是具有一个或多个类型变量的类,即创建用类型作为参数的类。
  • 如一个泛型类定义格式如下:class Generics<K,V>其中的K和V是类中的可变类型参数。
  • Pair类引入了一个类型变量T,用尖括号(<>)括起来,并放在类名的后面。
  • 泛型类可以有多个类型变量。例如:public class Pair<T, U> { … }
  • 类定义中的类型变量用于指定方法的返回类型以及域、局部变量的类型。
  • 泛型类的约束与局限性:
  1. 不能用基本类型实例化类型参数
  2. 运行时类型查询只适用于原始类型
  3. 不能抛出也不能捕获泛型类实例
  4. 参数化类型的数组不合法
  5. 不能实例化类型变量
  6. 泛型类的静态上下文中类型变量无效
  7. 注意擦除后的冲突

3、泛型方法的定义

  • 泛型方法:除了泛型类外,还可以只单独定义一个方法作为泛型方法,用于指定方法参数或者返回值为泛型类型,留待方法调用时确定。
  • 泛型方法可以声明在泛型类中,也可以声明在普通类中。
public class ArrayTool {
     public static<T> void insert(T[] e, int p)
     {
          //请自己添加代码
    }
 public static<T> T valueAt(T[] e , int p)
     {
          //请自己添加代码
      }
}

4、泛型接口的定义

public interfance IPool<T>
{
       T get();
       int add(T t);          
}

泛型变量的限定:

1.定义泛型变量的上界

extends关键字所声明的上界既可以是一个类,也可以是一个接口

2.定义泛型变量的下界

– 通过使用super关键字可以固定泛型参数的类型为某种类型或者其超类

– 当程序希望为一个方法的参数限定类型时,通常可以使用下限通配符

泛型类的约束与局限性(*)

不能用基本类型实例化类型参数

运行时类型查询只适用于原始类型

不能抛出也不能捕获泛型类实例

参数化类型的数组不合法

不能实例化类型变量

泛型类的静态上下文中类型变量无效注意擦除后的

泛型类型的继承规则(*)

Java中的数组是协变的(covariant)。

通配符类型

通配符

“?”符号表明参数的类型可以是任何一种类型,它和参数T的含义是有区别的。T表示一种未知类型,而“?”表示任何一种类型。这种通配符一般有以下三种用法:

– 单独的?,用于表示任何类型。

? extends type,表示带有上界。

? super type,表示带有下界。

第二部分:实验内容和步骤

实验1 导入第8章示例程序,测试程序并进行代码注释。

测试程序1:

编辑、调试、运行教材311312页代码,结合程序运行结果理解程序;

在泛型类定义及使用代码处添加注释;

掌握泛型类的定义及使用。 

代码如下:

 1 package pair1;
 2 
 3 /**
 4  * @version 1.00 2004-05-10
 5  * @author Cay Horstmann
 6  */
 7 public class Pair<T> //用户自定义泛型pair类
 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 }
Pair
 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" };
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 values, 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);// 具有最小值和最大值的对,如果a为null或空,则为null
36    }
37 }
PairTest1

运行结果:

  • Pair类引入了一个类型变量T,用尖括号(<>)括起来,并放在类名的后面。泛型类可以有多个类型变量。例如,可以定义Pair类,其中第一个域和第二个域使用不同的类型:
  • public class Pair<T,U> {......} 
  • 类定义中的类型变量指定方法的返回类型以及域和局部变量的类型。例如:private T first;  //uses the type variable
  • 用具体的类型替换类型变量就可以实例化泛型类型,例如:Pair<String> 

测试程序2:

编辑、调试运行教材315 PairTest2,结合程序运行结果理解程序;

在泛型程序设计代码处添加相关注释;

了解泛型方法、泛型变量限定的定义及用途。

 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 }
Pair
 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);//创建localdate类型的pair对象
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 values, or null if a is null or empty
32    */
33    public static <T extends Comparable> Pair<T> minmax(T[] a)//使用extends关键字,定义泛型变量的上界,调用Comparable接口
34    {
35       if (a == null || a.length == 0) return null;
36       T min = a[0];
37       T max = a[0];
38       for (int i = 1; i < a.length; i++)
39       {
40          if (min.compareTo(a[i]) > 0) min = a[i];
41          if (max.compareTo(a[i]) < 0) max = a[i];
42       }
43       return new Pair<>(min, max);
44    }
45 }
PairTest2

运行截图:

 

  • 为了让不同方法可以操作不同类型,而且类型还不确定。那么可以将泛型定义在方法上。
  • *extends E: 可以接收E类型或者E的子类型,上限。
  • *super E: 可以接收E类型或者E的父类型,下限。

测试程序3:

用调试运行教材335 PairTest3,结合程序运行结果理解程序;

了解通配符类型的定义及用途。

 1 package pair3;
 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 }
Pair
 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 }
Employee
 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 }
Manager
 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       var ceo = new Manager("Gus Greedy", 800000, 2003, 12, 15);
12       var cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15);
13       var buddies = new Pair<Manager>(ceo, cfo);      
14       printBuddies(buddies);
15 
16       ceo.setBonus(1000000);
17       cfo.setBonus(500000);
18       Manager[] managers = { ceo, cfo };
19 
20       var result = new Pair<Employee>();
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)//? extends type,表示带有上界
30    {
31       Employee first = p.getFirst();
32       Employee second = p.getSecond();
33       System.out.println(first.getName() + " and " + second.getName() + " are buddies.");//"?"在这儿是通配符,符号表明参数的类型可以是任何一种类型,它和参数T的含义是有区别的
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  //T表示一种未知类型,而“?”表示任何一种类型
50    public static void maxminBonus(Manager[] a, Pair<? super Manager> result)
51    {
52       minmaxBonus(a, result);
53       PairAlg.swapHelper(result); // swapHelper捕获通配符类型
54    }
55    // 不能写公共静态<T超级管理器> ...
56 }
57 
58 class PairAlg
59 {
60    public static boolean hasNulls(Pair<?> p)
61    {
62       return p.getFirst() == null || p.getSecond() == null;
63    }
64 
65    public static void swap(Pair<?> p) { swapHelper(p); }
66    
67 
68    public static <T> void swapHelper(Pair<T> p)
69    {
70       T t = p.getFirst();
71       p.setFirst(p.getSecond());
72       p.setSecond(t);
73    }
74 }
PairTest3

运行结果:

  •  为了表示各种泛型 List 的父类,可以使用类型通配符(?),将问号作为类型实参传给 List 集合,写作:List<?>(意思是元素类型未知的 List),此问号的元素类型可以匹配任何类型。

实验2结对编程练习,将程序提交到PTA2019面向对象程序设计基础知识测试题(2

1 编写一个泛型接口GeneralStack要求类方法对任何引用类型数据都适用。GeneralStack接口中方法如下:

push(item);            //如item为null,则不入栈直接返回null。

pop();                 //出栈,如为栈为空,则返回null。

peek();                //获得栈顶元素,如为空,则返回null.

public boolean empty();//如为空返回true

public int size();     //返回栈中元素数量

2定义GeneralStackArrayListGeneralStack要求:

ü 类内使用ArrayList对象存储堆栈数据,名为list

ü 方法: public String toString()//代码为return list.toString();

ü 代码中不要出现类型不安全的强制转换。

3定义Car类,类的属性

private int id;

private String name;

方法:Eclipse自动生成setter/getter,toString方法。

4main方法要求

ü 输入选项,有quit, Integer, Double, Car 4个选项。如果输入quit程序直接退出。否则,输入整数mnm代表入栈个数,n代表出栈个数。然后声明栈变量stack

ü 输入Integer,打印Integer Test。建立可以存放Integer类型的ArrayListGeneralStack。入栈m次,出栈n次。打印栈的toString方法。最后将栈中剩余元素出栈并累加输出。

ü 输入Double ,打印Double Test。剩下的与输入Integer一样。

ü 输入Car,打印Car Test。其他操作与IntegerDouble基本一样。只不过最后将栈中元素出栈,并将其name依次输出。

特别注意:如果栈为空,继续出栈,返回null

输入样例

Integer

5

2

1 2 3 4 5

Double

5

3

1.1 2.0 4.9 5.7 7.2

Car

3

2

1 Ford

2 Cherry

3 BYD

quit

 

输出样例

Integer Test

push:1

push:2

push:3

push:4

push:5

pop:5

pop:4

[1, 2, 3]

sum=6

interface GeneralStack

Double Test

push:1.1

push:2.0

push:4.9

push:5.7

push:7.2

pop:7.2

pop:5.7

pop:4.9

[1.1, 2.0]

sum=3.1

interface GeneralStack

Car Test

push:Car [id=1, name=Ford]

push:Car [id=2, name=Cherry]

push:Car [id=3, name=BYD]

pop:Car [id=3, name=BYD]

pop:Car [id=2, name=Cherry]

[Car [id=1, name=Ford]]

Ford

interface GeneralStack

 代码如下:

1 public interface GeneralStack<T> {
2     public T push(T item);  //如item为null,则不入栈直接返回null
3     public T pop();     //出栈,如为栈为空,则返回null。
4     public T peek();   //获得栈顶元素,如为空,则返回null.
5     public boolean empty();   //如为空返回true
6     public int size();    //返回栈中元素数量
7 }
GeneralStack
 1 public class ArrayListGeneralStack implements GeneralStack{
 2     ArrayList list =new ArrayList();
 3     
 4     public String toString(){
 5         return list.toString();
 6     }
 7     @Override
 8     public Object push(Object item) {
 9         // TODO Auto-generated method stub
10         if (list.add(item)){
11             return item;
12         }else {
13             return false;
14         }
15     }
16 
17     @Override
18     public Object pop() {
19         // TODO Auto-generated method stub
20         if (list.size()==0){
21             return null;
22         }
23         return list.remove(list.size()-1);
24     }
25 
26     @Override
27     public Object peek() {
28         // TODO Auto-generated method stub
29         return list.get(list.size()-1);
30     }
31 
32     @Override
33     public boolean empty() {
34         // TODO Auto-generated method stub
35         if (list.size()==0){
36             return true;
37         }else {
38             return false;
39         }
40     }
41 
42     @Override
43     public int size() {
44         // TODO Auto-generated method stub
45         return list.size();
46     }
47     
48 }
ArrayListGeneralStack
 1 public class Car{
 2     private int id;
 3     private String name;
 4     public Car(int id, String name) {
 5         this.id = id;
 6         this.name = name;
 7     }
 8     @Override
 9     public String toString() {
10         // TODO Auto-generated method stub
11         return  "Car [" +"id=" + id +", name=" + name  +"]";
12     }
13     public int getId() {
14         return id;
15     }
16     public void setId(int id) {
17         this.id = id;
18     }
19     public String getName() {
20         return name;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }    
25 }
Car
 1 public class Test {
 2  public static void main(String args[] ) {
 3      Scanner S=new Scanner(System.in);
 4      while(true) {
 5          String sc=S.nextLine();
 6           if(sc.equals("quit")) {
 7              break;
 8              }    
 9              else if(sc.equals("Integer")) {
10              System.out.println("Integer Test");
11              int count=S.nextInt();
12              int pop_time=S.nextInt();
13              ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();
14              for (int i=0;i<count;i++){
15                  System.out.println("push:"+arrayListGeneralStack.push(S.nextInt()));
16              }
17              for (int i=0;i<pop_time;i++){
18                  System.out.println("pop:"+arrayListGeneralStack.pop());
19              }
20              System.out.println(arrayListGeneralStack.toString());
21              int sum=0;
22              int size=arrayListGeneralStack.size();
23              for (int i=0;i<size;i++){
24                  sum+=(int)arrayListGeneralStack.pop();
25              }
26              System.out.println("sum="+sum);
27              System.out.println("interface GeneralStack");
28          }
29          else if (sc.equals("Double")) {
30              System.out.println("Double Test");
31              int count=S.nextInt();
32              int pop_time=S.nextInt();
33              ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();
34              for (int i=0;i<count;i++){
35                  System.out.println("push:"+arrayListGeneralStack.push(S.nextDouble()));
36              }
37              for (int i=0;i<pop_time;i++){
38                  System.out.println("pop:"+arrayListGeneralStack.pop());
39              }
40              System.out.println(arrayListGeneralStack.toString());
41              double sum=0;
42              int size=arrayListGeneralStack.size();
43              for (int i=0;i<size;i++){
44                  sum+=(double)arrayListGeneralStack.pop();
45              }
46              System.out.println("sum="+sum);
47              System.out.println("interface GeneralStack");
48          }
49          else if (sc.equals("Car")){
50              System.out.println("Car Test");
51              int count=S.nextInt();
52              int pop_time=S.nextInt();
53              ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();
54              for (int i=0;i<count;i++){
55                  int id=S.nextInt();
56                  String name=S.next();
57                  Car car = new Car(id,name);
58                  System.out.println("push:"+arrayListGeneralStack.push(car));
59              }
60              for (int i=0;i<pop_time;i++){
61                  System.out.println("pop:"+arrayListGeneralStack.pop());
62              }
63              System.out.println(arrayListGeneralStack.toString());
64              if (arrayListGeneralStack.size()>0){
65                  int size=arrayListGeneralStack.size();
66                  for (int i=0;i<size;i++){
67                      Car car=(Car) arrayListGeneralStack.pop();
68                      System.out.println(car.getName());
69                  }
70              }
71              System.out.println("interface GeneralStack");
72          }
73         
74          }
75       
76   }
77 }
Test

整体代码如下:

  1 package week11PartnerProgrem;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Scanner;
  5 
  6 interface GeneralStack<T> {
  7     public T push(T item);  //如item为null,则不入栈直接返回null
  8     public T pop();     //出栈,如为栈为空,则返回null。
  9     public T peek();   //获得栈顶元素,如为空,则返回null.
 10     public boolean empty();   //如为空返回true
 11     public int size();    //返回栈中元素数量
 12 }
 13 class ArrayListGeneralStack implements GeneralStack{
 14     ArrayList list =new ArrayList();
 15     
 16     public String toString(){
 17         return list.toString();
 18     }
 19     @Override
 20     public Object push(Object item) {
 21         // TODO Auto-generated method stub
 22         if (list.add(item)){
 23             return item;
 24         }else {
 25             return false;
 26         }
 27     }
 28 
 29     @Override
 30     public Object pop() {
 31         // TODO Auto-generated method stub
 32         if (list.size()==0){
 33             return null;
 34         }
 35         return list.remove(list.size()-1);
 36     }
 37 
 38     @Override
 39     public Object peek() {
 40         // TODO Auto-generated method stub
 41         return list.get(list.size()-1);
 42     }
 43 
 44     @Override
 45     public boolean empty() {
 46         // TODO Auto-generated method stub
 47         if (list.size()==0){
 48             return true;
 49         }else {
 50             return false;
 51         }
 52     }
 53 
 54     @Override
 55     public int size() {
 56         // TODO Auto-generated method stub
 57         return list.size();
 58     }
 59     
 60 }
 61 
 62 class Car{
 63     private int id;
 64     private String name;
 65     public Car(int id, String name) {
 66         this.id = id;
 67         this.name = name;
 68     }
 69     @Override
 70     public String toString() {
 71         // TODO Auto-generated method stub
 72         return  "Car [" +"id=" + id +", name=" + name  +"]";
 73     }
 74     public int getId() {
 75         return id;
 76     }
 77     public void setId(int id) {
 78         this.id = id;
 79     }
 80     public String getName() {
 81         return name;
 82     }
 83     public void setName(String name) {
 84         this.name = name;
 85     }    
 86 }
 87 public class Test {
 88  public static void main(String args[] ) {
 89      Scanner S=new Scanner(System.in);
 90      while(true) {
 91          String sc=S.nextLine();
 92           if(sc.equals("quit")) {
 93              break;
 94              }    
 95              else if(sc.equals("Integer")) {
 96              System.out.println("Integer Test");
 97              int count=S.nextInt();
 98              int pop_time=S.nextInt();
 99              ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();
100              for (int i=0;i<count;i++){
101                  System.out.println("push:"+arrayListGeneralStack.push(S.nextInt()));
102              }
103              for (int i=0;i<pop_time;i++){
104                  System.out.println("pop:"+arrayListGeneralStack.pop());
105              }
106              System.out.println(arrayListGeneralStack.toString());
107              int sum=0;
108              int size=arrayListGeneralStack.size();
109              for (int i=0;i<size;i++){
110                  sum+=(int)arrayListGeneralStack.pop();
111              }
112              System.out.println("sum="+sum);
113              System.out.println("interface GeneralStack");
114          }
115          else if (sc.equals("Double")) {
116              System.out.println("Double Test");
117              int count=S.nextInt();
118              int pop_time=S.nextInt();
119              ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();
120              for (int i=0;i<count;i++){
121                  System.out.println("push:"+arrayListGeneralStack.push(S.nextDouble()));
122              }
123              for (int i=0;i<pop_time;i++){
124                  System.out.println("pop:"+arrayListGeneralStack.pop());
125              }
126              System.out.println(arrayListGeneralStack.toString());
127              double sum=0;
128              int size=arrayListGeneralStack.size();
129              for (int i=0;i<size;i++){
130                  sum+=(double)arrayListGeneralStack.pop();
131              }
132              System.out.println("sum="+sum);
133              System.out.println("interface GeneralStack");
134          }
135          else if (sc.equals("Car")){
136              System.out.println("Car Test");
137              int count=S.nextInt();
138              int pop_time=S.nextInt();
139              ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();
140              for (int i=0;i<count;i++){
141                  int id=S.nextInt();
142                  String name=S.next();
143                  Car car = new Car(id,name);
144                  System.out.println("push:"+arrayListGeneralStack.push(car));
145              }
146              for (int i=0;i<pop_time;i++){
147                  System.out.println("pop:"+arrayListGeneralStack.pop());
148              }
149              System.out.println(arrayListGeneralStack.toString());
150              if (arrayListGeneralStack.size()>0){
151                  int size=arrayListGeneralStack.size();
152                  for (int i=0;i<size;i++){
153                      Car car=(Car) arrayListGeneralStack.pop();
154                      System.out.println(car.getName());
155                  }
156              }
157              System.out.println("interface GeneralStack");
158          }
159         
160          }
161       
162   }
163 }
Test

运行截图:

 

第三部分:

  • 实验心得:

    本章我们学习了泛型程序设计技术,理解了泛型概念,掌握了泛型类的定义与使用和泛型方法的声明与使用。在验证实验中,通过对代码的解读,更好的学习了泛型程序<T>的使用,虽然不是很熟练,但还是做出来了,感觉依旧存在些小问题,在接下来的实验中要更好的解决。还有在自己编程时,还存在很大问题。在之后的学习中,我会多练习程序去了解这些知识。

posted on 2019-11-11 20:07  ApeJ  阅读(195)  评论(1编辑  收藏  举报