zoukankan      html  css  js  c++  java
  • 李瑞红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 + "	" + sex + "	" + age + "	" + number + "	" + province + "
    ";
    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>代表下边界限定通配符。

    感受:

    通过本周的学习,掌握了泛型类的定义,以及泛型方法的声明,还有泛型接口的定义,以及对泛型变量的限定。但在用泛型类写程序时还是有一点点的困难。

    在自己编程时,还是有很大问题。在之后的学习中,我会多练习程序去了解这些知识,争取能够独立完整的编写程序。

     
     
     
    好文要顶 关注我 收藏该文  
  • 相关阅读:
    zabbix
    Tomcat 部署Web应用
    WebLogic
    WebLogic部署集群和代理服务器
    WebLogic 中的基本概念
    GIT详解和GITHUB
    Tomcat 内存溢出对应解决方式
    apache和tomcat区别
    数据库介绍
    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
  • 原文地址:https://www.cnblogs.com/LRHLRH123----/p/9904455.html
Copyright © 2011-2022 走看看