• 201874040116-李鑫《面向对象程序设计(java)》第十一周学习总结


    项目

    内容

    这个作业属于哪个课程

     https://www.cnblogs.com/nwnu-daizh/

    这个作业的要求在哪里

    https://www.cnblogs.com/nwnu-daizh/p/11815810.html

    作业学习目标

    1. 理解泛型概念;
    2. 掌握泛型类的定义与使用;
    3. 掌握泛型方法的声明与使用;
    4. 掌握泛型接口的定义与实现;
    5. 了解泛型程序设计,理解其用途。

    第一部分:总结第八章关于泛型程序设计理论知识:

    1.Java 泛型

      1)Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。

      2)泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。

      3)泛型程序设计(Generic programming) 意味着编写的代码可以被很多不同类型的对象所重用。就像 ArrayList 类一样。

    2.泛型类

      1)泛型类就是具有一个或多个类型变量的类。

      2)定义泛型类的关键在于保证类的通用性和低限制。

    3.泛型方法

      1)泛型方法不一定要定义在泛型类中。

      2)可以通过限定类型的继承与接口的实现关系来确保接受的数据类型是合法的。

    4.约束与局限性

      1)不能使用基本类型实例化类型参数。

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

      3)不能创建参数化类型的数组。

      4)不能构造泛型数组。

      5)泛型类的静态上下文中类型变量无效。

      6)不能抛出或捕获泛型类的实例。

      7)可以消除对受查异常的检查。

      8)当泛型类型被擦除时,无法创建引发冲突的条件。

    5.通配符类型

      1)使用?代替具体的类型参数来表示可接受的类型。

      2)子类型限定: Pair<? extends Employee>

      3)超类限定:Pair<? super Manager>

      4)实现接口限定:<T extends Comparable>

    第二部分:实验部分

    实验1:测试程序1

     

     1 public class PairTest1
     2 {
     3    public static void main(String[] args)
     4    {
     5       String[] words = { "Mary", "had", "a", "little", "lamb" };
     6       Pair<String> mm = ArrayAlg.minmax(words);        //构建一个属性的数据类型为 String 的泛型类
     7       System.out.println("min = " + mm.getFirst());
     8       System.out.println("max = " + mm.getSecond());
     9    }
    10 }
    11 
    12 class ArrayAlg
    13 {
    14   
    15    public static Pair<String> minmax(String[] a)    //返回类型为 属性的数据类型为  String 的泛型类
    16    {
    17       if (a == null || a.length == 0) return null;
    18       String min = a[0];
    19       String max = a[0];
    20       for (int i = 1; i < a.length; i++)
    21       {
    22          if (min.compareTo(a[i]) > 0) min = a[i];
    23          if (max.compareTo(a[i]) < 0) max = a[i];
    24       }
    25       return new Pair<>(min, max);
    26    }
    27 }
    28 public class Pair<T>     //属性只有一种数据类型的泛型类
    29 {
    30    private T first;
    31    private T second;
    32 
    33    public Pair() { first = null; second = null; }
    34    public Pair(T first, T second) { this.first = first;  this.second = second; }
    35 
    36    public T getFirst() { return first; }
    37    public T getSecond() { return second; }
    38 
    39    public void setFirst(T newValue) { first = newValue; }
    40    public void setSecond(T newValue) { second = newValue; }
    41 }

    测试程序2

     

     1 import java.time.*;
     2 
     3 public class PairTest2
     4 {
     5    public static void main(String[] args)
     6    {
     7       LocalDate[] birthdays = 
     8          { 
     9             LocalDate.of(1906, 12, 9), // G. Hopper
    10             LocalDate.of(1815, 12, 10), // A. Lovelace
    11             LocalDate.of(1903, 12, 3), // J. von Neumann
    12             LocalDate.of(1910, 6, 22), // K. Zuse
    13          };
    14       Pair<LocalDate> mm = ArrayAlg.minmax(birthdays);    //实例化LocalDate类型的变量 mm 
    15       System.out.println("min = " + mm.getFirst());
    16       System.out.println("max = " + mm.getSecond());
    17    }
    18 }
    19 
    20 class ArrayAlg
    21 {
    22    /**
    23       Gets the minimum and maximum of an array of objects of type T.
    24       @param a an array of objects of type T
    25       @return a pair with the min and max values, or null if a is null or empty
    26    */
    27    public static <T extends Comparable> Pair<T> minmax(T[] a)     //返回类型限定为实现了Comparable接口的类型   的泛型方法
    28    {
    29       if (a == null || a.length == 0) return null;
    30       T min = a[0];
    31       T max = a[0];
    32       for (int i = 1; i < a.length; i++)
    33       {
    34          if (min.compareTo(a[i]) > 0) min = a[i];
    35          if (max.compareTo(a[i]) < 0) max = a[i];
    36       }
    37       return new Pair<>(min, max);
    38    }
    39 }
    40 public class Pair<T> //泛型类Pair
    41 {
    42    private T first;
    43    private T second;
    44 
    45    public Pair() { first = null; second = null; }
    46    public Pair(T first, T second) { this.first = first;  this.second = second; }
    47 
    48    public T getFirst() { return first; }
    49    public T getSecond() { return second; }
    50 
    51    public void setFirst(T newValue) { first = newValue; }
    52    public void setSecond(T newValue) { second = newValue; }
    53 }

    测试程序3

     

      1 public class PairTest3
      2 {
      3    public static void main(String[] args)
      4    {
      5       var ceo = new Manager("Gus Greedy", 800000, 2003, 12, 15);
      6       var cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15);
      7       var buddies = new Pair<Manager>(ceo, cfo);      
      8       printBuddies(buddies);            
      9 
     10       ceo.setBonus(1000000);
     11       cfo.setBonus(500000);
     12       Manager[] managers = { ceo, cfo };
     13 
     14       var result = new Pair<Employee>();
     15       minmaxBonus(managers, result);    
     16       System.out.println("first: " + result.getFirst().getName() 
     17          + ", second: " + result.getSecond().getName());
     18       maxminBonus(managers, result);
     19       System.out.println("first: " + result.getFirst().getName() 
     20          + ", second: " + result.getSecond().getName());
     21    }
     22 
     23    public static void printBuddies(Pair<? extends Employee> p) //限定参数类型为Employee的子类的泛型方法
     24    {
     25       Employee first = p.getFirst();
     26       Employee second = p.getSecond();
     27       System.out.println(first.getName() + " and " + second.getName() + " are buddies.");
     28    }
     29 
     30    public static void minmaxBonus(Manager[] a, Pair<? super Manager> result) //限定参数类型为 Manager 的超类的泛型方法
     31    {
     32       if (a.length == 0) return;
     33       Manager min = a[0];
     34       Manager max = a[0];
     35       for (int i = 1; i < a.length; i++)
     36       {
     37          if (min.getBonus() > a[i].getBonus()) min = a[i];
     38          if (max.getBonus() < a[i].getBonus()) max = a[i];
     39       }
     40       result.setFirst(min);
     41       result.setSecond(max);
     42    }
     43 
     44    public static void maxminBonus(Manager[] a, Pair<? super Manager> result) //限定参数类型为 Manager 的超类的泛型方法
     45    {
     46       minmaxBonus(a, result);
     47       PairAlg.swapHelper(result); // OK--swapHelper captures wildcard type
     48    }
     49    // can't write public static <T super manager> . . .
     50 }
     51 
     52 class PairAlg
     53 {
     54    public static boolean hasNulls(Pair<?> p)    
     55    {
     56       return p.getFirst() == null || p.getSecond() == null;
     57    }
     58 
     59    public static void swap(Pair<?> p) { swapHelper(p); }
     60 
     61    public static <T> void swapHelper(Pair<T> p)
     62    {
     63       T t = p.getFirst();
     64       p.setFirst(p.getSecond());
     65       p.setSecond(t);
     66    }
     67 }
     68 public class Pair<T>     //泛型类
     69 {
     70    private T first;
     71    private T second;
     72 
     73    public Pair() { first = null; second = null; }
     74    public Pair(T first, T second) { this.first = first;  this.second = second; }
     75 
     76    public T getFirst() { return first; }
     77    public T getSecond() { return second; }
     78 
     79    public void setFirst(T newValue) { first = newValue; }
     80    public void setSecond(T newValue) { second = newValue; }
     81 }
     82 
     83 import java.time.*;
     84 
     85 public class Employee
     86 {  
     87    private String name;
     88    private double salary;
     89    private LocalDate hireDay;
     90 
     91    public Employee(String name, double salary, int year, int month, int day)
     92    {
     93       this.name = name;
     94       this.salary = salary;
     95       hireDay = LocalDate.of(year, month, day);
     96    }
     97 
     98    public String getName()
     99    {
    100       return name;
    101    }
    102 
    103    public double getSalary()
    104    {  
    105       return salary;
    106    }
    107 
    108    public LocalDate getHireDay()
    109    {  
    110       return hireDay;
    111    }
    112 
    113    public void raiseSalary(double byPercent)
    114    {  
    115       double raise = salary * byPercent / 100;
    116       salary += raise;
    117    }
    118 }
    119 
    120 public class Manager extends Employee    //继承自Employee类
    121 {  
    122    private double bonus;
    123 
    124    /**
    125       @param name the employee's name
    126       @param salary the salary
    127       @param year the hire year
    128       @param month the hire month
    129       @param day the hire day
    130    */
    131    public Manager(String name, double salary, int year, int month, int day)
    132    {  
    133       super(name, salary, year, month, day);
    134       bonus = 0;
    135    }
    136 
    137    public double getSalary()
    138    { 
    139       double baseSalary = super.getSalary();
    140       return baseSalary + bonus;
    141    }
    142 
    143    public void setBonus(double b)
    144    {  
    145       bonus = b;
    146    }
    147 
    148    public double getBonus()
    149    {  
    150       return bonus;
    151    }
    152 }

    实验2:结对编程练习

     

    实验总结:

      学习了 泛型 这一java特性;了解了定义泛型类,泛型方法需要注意的关键点,以及如何限定泛型类型。

     

  • 相关阅读:
    ASP.NET MVC随想录——锋利的KATANA
    ASP.NET MVC随想录——漫谈OWIN
    Notepad++ 64位 插件管理
    C#实现按键精灵的'找图' '找色' '找字'的功能
    http://www.haolizi.net/example/view_2380.html
    C# 关于在原图中寻找子图片坐标的类
    WebBrowser控件默认使用IE9,IE10的方法
    Springboot---后台导出功能,easyExcel
    vue---EleElement UI 表格导出功能
    vue---提取公共方法
  • 原文地址:https://www.cnblogs.com/whitepaint/p/11818593.html
Copyright © 2020-2023  润新知