• 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+"	"+sex+"	"+age+"	"+number+"	"+province+"
    ";
    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并不代表继
    承,它是类型范围限制。

  • 相关阅读:
    gitlab 重置密码
    _string 灵活查询
    删除前面,删除后面
    HTTPS
    PHP正则匹配价格
    PHP LUHN算法验证银行卡
    PHP 与操作判断奇偶
    PHP与Cookie
    检查字符串是否存在
    php密码正则匹配
  • 原文地址:https://www.cnblogs.com/wy201771010126/p/9890505.html
Copyright © 2020-2023  润新知