201871010106-丁宣元 《面向对象程序设计(java)》第八周学习总结

正文开头:

项目

内容

这个作业属于哪个课程

https://home.cnblogs.com/u/nwnu-daizh/

这个作业的要求在哪里

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

作业学习目标

(1) 掌握接口定义方法;

(2) 掌握实现接口类的定义要求;

(3) 掌握实现了接口类的使用要求;

(4) 掌握程序回调设计模式;

(5) 掌握Comparator接口用法;

(6) 掌握对象浅层拷贝与深层拷贝方法;

(7) 掌握Lambda表达式语法;

(8) 了解内部类的用途及语法要求。

正文内容:

第一部分:总结第六章理论知识

6.1  接口

1.接口是对类的一组需求描述,这些类要遵从接口描述的统一格式进行定义

 2.Java为了克服单继承性的缺点,使用接口。一个类可以实现多个接口,接口中不包括变量和具体实现方法

 3.类型:用户自定义接口,标准接口

4.声明:public interface 接口名    注:包括常量定义和抽象方法定义,允许方法声明不允许方法实现

 5.接口可以扩展:class interface 接口1 extends 接口2

  接口中所有常量为public static finial,方法为public abstract,接口中的所有方法自动地属于public

 6.实现:implements

    eg:class Employee implements Printable

    注:一个类实现了某个接口,则类实现该接口的所有方法,可以实现多个接口

 7.使用:

  接口不能构造接口对象,但可以声明接口变量以指向类对象

  instanceof检查是否实现了某个接口

    if (anObject instanceof Comparable)
    {······ }

 8.接口与抽象类:(PPT)
  (1)抽象类:用abstract来声明,没有具体实例对象的类,不能用new来创建对象。可包含常规类所包含的任何东西,抽象类必须由子类继承,如果abstract类的子类不是抽象类,那么子类必须重写父类中的所有的abstract方法;
  (2)接口:用interface来声明,是抽象方法和常量值构成的集合。接口是一种特殊的抽象类,这种抽象类中只包含常量和抽象方法的定义,而没有变量和方法的定义,接口中只能定义抽象方法,而且这些方法默认为public的,只要类实现了接口,就可以在任何需要该接口的地方使用这个类的对象。一个类可以实现多个接口。
  9.接口与抽象类的区别:
  (1)接口不能实现任何方法,而抽象类可以实现
  (2)类可以实现很多接口,一个父类
  (3)接口不是一个类分级结构中的一部分,无任何联系的类可以实现相同的接口

6.2 接口示例
  1.接口与回调
    a回调:(callback):一种程序设计模式。可指出某个特定事件发生时,程序应该采取的动作
    bTimer类,可以使用它在到达给定的时间间隔时触发的一个事件。
  2.Comparable接口:处理字符串按长度进行排序的操作
  3.对象克隆:
    a.object类的clone方法:当拷贝一个对象变量时,原始变量与拷贝变量引用同一个对象,这样,改变一个变量所引用的对象会对另一个变量产生影响;如果要创建一个对象新的copy,它的最初状态与original一样,但以后可以各自改变状态,就需要使用clone方法;object类中的clone()方法是一个native方法; object类中的clone()方法被protected修饰符修饰,着以为着在用户编写的代码中不能直接调用它。如果要直接应用clone()方法,就需要覆盖clone()方法,并要把clone()方法的属性设置为public;object类中的clone()方法返回一个object对象,必须进行强制转换类型才能得到需要的类型;
  2、浅层拷贝和深层拷贝
    浅层拷贝:被拷贝对象的所有常量成员和基本类型属性都有与原来对象相同的拷贝值,而若成员域是一个对象,则被拷贝对象该对象鱼的对象引用仍然指向原来的对象;
    深层拷贝:被拷贝对象的所有成员域都含有与原来对象相同的拷贝值,且对象域将指向被复制过的新对象,而不是原来对象被引用的而对象。深层拷贝将拷贝对象内引用的对象也拷贝一遍
  3、Java中克隆对象的实现:
    (1)子类中实现Cloneable接口
    (2)为了获取对象的一份拷贝,可以利用Object类的clone方法
    (3)子类中覆盖超类的clone方法,声明为public
    (4)子类的clone方法中,调用super.clone()
6.3 Lambda表达式:a.提供一个函数化的语法来简化编码。
     b.表达式本质上是一个匿名方法。
     public int add(int x, int y){
     return x + y;

          }

    将其转成Lambda表达式为: (int x,int y) -> x + y;
     c.基本结构: (arguments)->  body
6.4 内部类
  内部类(inner class ):定义在一个类内部的类,外层的类称为外部类(outer class),内部类主要用于处理事件;
  声明:
  class outerClass{
  【修饰符】class inner Class{
  ...........
  }

  匿名内部类:只创建类的一个对象,不必为为该类命名

  静态内部类:static修饰一个内部类,则相当于是一个外部定义的类。(少用)

  static的内部类中可以声明static成员,但非static的内部类中成员不能声明为static。

实验内容和步骤:

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

测试程序1:

  编辑、编译、调试运行阅读教材214页-215页程序6-1、6-2,理解程序并分析程序运行结果;

     在程序中相关代码处添加新知识的注释。

     掌握接口的实现用法;

     掌握内置接口Compareable的用法。

代码:Employee.java

  1. package interfaces;
  2.  
  3. public class Employee implements Comparable<Employee>//Employee类实现Comparable接口,并为泛型Comparable接口提供一个类型参数
  4. {
  5. private String name;
  6. private double salary;
  7.  
  8. public Employee(String name, double salary)//构造器
  9. {
  10. this.name = name;
  11. this.salary = salary;
  12. }
  13.  
  14. public String getName()//访问器
  15. {
  16. return name;
  17. }
  18.  
  19. public double getSalary()//访问器
  20. {
  21. return salary;
  22. }
  23.  
  24. public void raiseSalary(double byPercent)
  25. {
  26. double raise = salary * byPercent / 100;
  27. salary += raise;
  28. }
  29.  
  30. /**
  31. * Compares employees by salary
  32. * @param other another Employee object
  33. * @return a negative value if this employee has a lower salary than
  34. * otherObject, 0 if the salaries are the same, a positive value otherwise
  35. */
  36. public int compareTo(Employee other)//Employee类的compareTo方法
  37. {
  38. return Double.compare(salary, other.salary);//Double的compare方法,比较对象是salary
  39. }
  40. }

EmployeeSortTest.java

  1. package interfaces;
  2.  
  3. import java.util.*;
  4.  
  5. /**
  6. * This program demonstrates the use of the Comparable interface.
  7. * @version 1.30 2004-02-27
  8. * @author Cay Horstmann
  9. */
  10. public class EmployeeSortTest
  11. {
  12. public static void main(String[] args)
  13. {
  14. Employee[] staff = new Employee[3];//创建数组
  15.  
  16. staff[0] = new Employee("Harry Hacker", 35000);
  17. staff[1] = new Employee("Carl Cracker", 75000);
  18. staff[2] = new Employee("Tony Tester", 38000);
  19.  
  20. Arrays.sort(staff);//调用Arrays类的sort方法进行排序,Employee要实现Comparable接口
  21.  
  22. // print out information about all Employee objects
  23. for (Employee e : staff)//foreach循环遍历打印输出信息
  24. System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
  25. }
  26. }

结果:

测试程序2:

    编辑、编译、调试以下程序,结合程序运行结果理解程序;

代码:

  1. interface A //A是一个接口
  2. {
  3. double g=9.8;
  4. void show( );
  5. }
  6. class C implements A //C类实现A接口
  7. {
  8. public void show( )
  9. {System.out.println("g="+g);}
  10. }
  11.  
  12. class InterfaceTest
  13. {
  14. public static void main(String[ ] args)
  15. {
  16. A a=new C( );
  17. a.show( );
  18. System.out.println("g="+C.g);//用C调用A中的变量
  19. }
  20. }

结果:

测试程序3:

  在elipse IDE中调试运行教材223页6-3,结合程序运行结果理解程序;

  26行、36行代码参阅224页,详细内容涉及教材12章。

  在程序中相关代码处添加新知识的注释。

  掌握回调程序设计模式;

代码:

  1. package timer;
  2.  
  3. /**
  4. @version 1.02 2017-12-14
  5. @author Cay Horstmann
  6. */
  7.  
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.time.*;
  11. import javax.swing.*;
  12.  
  13. public class TimerTest
  14. {
  15. public static void main(String[] args)
  16. {
  17. ActionListener listener = new TimePrinter();
  18.  
  19. // construct a timer that calls the listener创建listener的timer数组
  20. // once every second
  21. Timer timer = new Timer(10000, listener);//创建定时器类对象timer
  22. timer.start();//启动定时器
  23.  
  24. // keep program running until the user selects "OK"
  25. JOptionPane.showMessageDialog(null, "Quit program?");//启动程序后,会弹出"Quit program?"
  26. System.exit(0);
  27. }
  28. }
  29.  
  30. class TimePrinter implements ActionListener//TimePrinter类实现ActionListener接口
  31. {
  32. public void actionPerformed(ActionEvent event)
  33. {
  34. System.out.println("At the tone, the time is " //每隔10秒,信息输出一次,响一声铃
  35. + Instant.ofEpochMilli(event.getWhen()));
  36. Toolkit.getDefaultToolkit().beep();//调用getDefaultToolkit()方法
  37. }
  38. }

结果:

回调:客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数。

测试程序4:

  调试运行教材229页-231页程序6-4、6-5,结合程序运行结果理解程序;

  在程序中相关代码处添加新知识的注释。

     掌握对象克隆实现技术;

  掌握浅拷贝和深拷贝的差别。

代码:

CloneTest.java

  1. package clone;
  2.  
  3. /**
  4. * This program demonstrates cloning.
  5. * @version 1.11 2018-03-16
  6. * @author Cay Horstmann
  7. */
  8. public class CloneTest
  9. {
  10. public static void main(String[] args) throws CloneNotSupportedException
  11. {
  12. Employee original = new Employee("John Q. Public", 50000);
  13. original.setHireDay(2000, 1, 1);
  14. Employee copy = original.clone();//原始变量与拷贝变量引用统一个对象
  15. copy.raiseSalary(10); //copy会增长10%的薪资
  16. copy.setHireDay(2002, 12, 31);
  17. System.out.println("original=" + original);
  18. System.out.println("copy=" + copy);
  19. }
  20. }

Employee.java

  1. package clone;
  2.  
  3. import java.util.Date;
  4. import java.util.GregorianCalendar;
  5.  
  6. public class Employee implements Cloneable
  7. {
  8. private String name;
  9. private double salary;
  10. private Date hireDay;
  11.  
  12. public Employee(String name, double salary)//Employee构造器
  13. {
  14. this.name = name;
  15. this.salary = salary;
  16. hireDay = new Date();
  17. }
  18.  
  19. public Employee clone() throws CloneNotSupportedException//声明异常,在一个对象上调用clone,但没有实现Cloneable的接口,Object类的clone方法就会throw出CloneNotSupportedException
  20. {
  21. // call Object.clone()调用对象克隆
  22. Employee cloned = (Employee) super.clone();
  23.  
  24. // clone mutable fields
  25. cloned.hireDay = (Date) hireDay.clone();
  26.  
  27. return cloned;
  28. }
  29.  
  30. /**
  31. * Set the hire day to a given date.
  32. * @param year the year of the hire day
  33. * @param month the month of the hire day
  34. * @param day the day of the hire day
  35. */
  36. public void setHireDay(int year, int month, int day)
  37. {
  38. Date newHireDay = new GregorianCalendar(year, month - 1, day).getTime();
  39.  
  40. // example of instance field mutation实例字段突变
  41. hireDay.setTime(newHireDay.getTime());
  42. }
  43.  
  44. public void raiseSalary(double byPercent)
  45. {
  46. double raise = salary * byPercent / 100;
  47. salary += raise;
  48. }
  49.  
  50. public String toString()
  51. {
  52. return "Employee[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
  53. }
  54. }

结果:

注:浅层拷贝和深层拷贝的区别:PPT

    浅层拷贝:被拷贝对象的所有常量成员和基本类型属性都有与原来对象相同的拷贝值,而若成员域是一个对象,则被拷贝对象该对象鱼的对象引用仍然指向原来的对象;
    深层拷贝:被拷贝对象的所有成员域都含有与原来对象相同的拷贝值,且对象域将指向被复制过的新对象,而不是原来对象被引用的而对象。深层拷贝将拷贝对象内引用的对象也拷贝一遍

实验2: 导入第6章示例程序6-6,学习Lambda表达式用法。

  调试运行教材233页-234页程序6-6,结合程序运行结果理解程序;

  在程序中相关代码处添加新知识的注释。

  将27-29行代码与教材223页程序对比,将27-29行代码与此程序对比,体会Lambda表达式的优点。

代码:

  1. package lambda;
  2.  
  3. import java.util.*;
  4.  
  5. import javax.swing.*;
  6. import javax.swing.Timer;
  7.  
  8. /**
  9. * This program demonstrates the use of lambda expressions.
  10. * @version 1.0 2015-05-12
  11. * @author Cay Horstmann
  12. */
  13. public class LambdaTest
  14. {
  15. public static void main(String[] args)
  16. {
  17. String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
  18. "Jupiter", "Saturn", "Uranus", "Neptune" };
  19. System.out.println(Arrays.toString(planets)); //调用Arrays的toString方法,并将planets输出在控制台上
  20. System.out.println("Sorted in dictionary order:");
  21. Arrays.sort(planets);//排序
  22. System.out.println(Arrays.toString(planets));
  23. System.out.println("Sorted by length:");
  24. Arrays.sort(planets, (first, second) -> first.length() - second.length());//推导出first,second的类型
  25. System.out.println(Arrays.toString(planets));
  26.  
  27. Timer timer = new Timer(1000, event ->
  28. System.out.println("The time is " + new Date()));
  29. timer.start(); //启动计时器
  30.  
  31. // keep program running until user selects "OK" 运行直至点击OK
  32. JOptionPane.showMessageDialog(null, "Quit program?");
  33. System.exit(0);
  34. }
  35. }

结果:

注:lambda表达式的优缺点:

    优:简洁明了,易计算,优化了代码,减少很多代码。

    缺:难理解

 实验3: 编程练习

  编制一个程序,将身份证号.txt 中的信息读入到内存中;

  按姓名字典序输出人员信息;

  查询最大年龄的人员信息;

  查询最小年龄人员信息;

  输入你的年龄,查询身份证号.txt中年龄与你最近人的姓名、身份证号、年龄、性别和出生地;

  查询人员中是否有你的同乡。

代码:

    Identify.java

  1. package IdentifySearch;
  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.Scanner;
  13.  
  14. public class Identify {
  15. private static ArrayList<Person> personlist;
  16.  
  17. public static void main(String[] args) {
  18. personlist = new ArrayList<>();
  19. Scanner scanner = new Scanner(System.in);
  20. File file = new File("E:\\身份证号.txt");
  21. try {
  22. FileInputStream fis = new FileInputStream(file);
  23. BufferedReader in = new BufferedReader(new InputStreamReader(fis));
  24. String temp = null;
  25. while ((temp = in.readLine()) != null) {
  26.  
  27. Scanner linescanner = new Scanner(temp);
  28.  
  29. linescanner.useDelimiter(" ");
  30. String name = linescanner.next();
  31. String number = linescanner.next();
  32. String sex = linescanner.next();
  33. String age = linescanner.next();
  34. String hometown = linescanner.nextLine();
  35. Person person = new Person();
  36. person.setName(name);
  37. person.setnumber(number);
  38. person.setsex(sex);
  39. int A = Integer.parseInt(age);
  40. person.setage(A);
  41. person.sethometown(hometown);
  42. personlist.add(person);
  43.  
  44. }
  45. } catch (FileNotFoundException e) {
  46. System.out.println("身份信息文件找不到");
  47. e.printStackTrace();
  48. } catch (IOException e) {
  49. System.out.println("身份信息文件读取错误");
  50. e.printStackTrace();
  51. }
  52. boolean isTrue = true;
  53. while (isTrue) {
  54. System.out.println("0.按姓名字典序输出人员信息;");
  55. System.out.println("1.查询最大年龄人员信息;;");
  56. System.out.println("2.查询最小年龄人员信息;");
  57. System.out.println("3.寻找同乡;");
  58. System.out.println("4.寻找年龄相近的人;");
  59. System.out.println("5.退出。");
  60. String W = scanner.next();
  61. switch (W) {
  62. case "0":
  63. Collections.sort(personlist);
  64. System.out.println(personlist.toString());
  65. break;
  66. case "1":
  67. int a = 0;
  68. int j, c1 = 0, d1 = 0;
  69. for (int i = 1; i < personlist.size(); i++) {
  70. j = personlist.get(i).getage();
  71. if (j > a) {
  72. a = j;
  73. c1 = i;
  74. }
  75. }
  76. System.out.println("年龄最大:" + personlist.get(c1));
  77. break;
  78. case "2":
  79. int b = 100;
  80. int c2 = 0,d2 = 0;
  81. for (int i = 1; i < personlist.size(); i++) {
  82. j = personlist.get(i).getage();
  83. if (j < b) {
  84. b = j;
  85. d2 = i;
  86. }
  87. }
  88. System.out.println("年龄最小:" + personlist.get(d2));
  89. break;
  90. case "3":
  91. System.out.println("籍贯:");
  92. String search = scanner.next();
  93. String place = search.substring(0, 3);
  94. int i = 0;
  95. for (; i < personlist.size(); i++) {
  96. if (personlist.get(i).gethometown().substring(1, 4).equals(place))
  97. System.out.println("你的同乡是:" + personlist.get(i));
  98. }
  99. break;
  100. case "4":
  101. System.out.println("年龄:");
  102. int yourage = scanner.nextInt();
  103. int nearaga = agenear(yourage);
  104. int value = yourage - personlist.get(nearaga).getage();
  105. System.out.println("" + personlist.get(nearaga));
  106. break;
  107. case "5":
  108. isTrue = false;
  109. System.out.println("退出程序!");
  110. break;
  111. default:
  112. System.out.println("检查输入!");
  113. }
  114. }
  115. }
  116.  
  117. public static int agenear(int age) {
  118. int j = 0, b = 53, value = 0, c = 0;
  119. for (int i = 0; i < personlist.size(); i++) {
  120. value = personlist.get(i).getage() - age;
  121. if (value < 0)
  122. value = -value;
  123. if (value < b) {
  124. b = value;
  125. c = i;
  126. }
  127. }
  128. return c;
  129. }
  130. }

    Person.java

  1. package IdentifySearch;
  2.  
  3. public class Person implements Comparable<Person> {
  4.  
  5. private String name;
  6. private String number ;
  7. private String sex ;
  8. private int age;
  9. private String hometown;
  10.  
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public String getnumber() {
  18. return number;
  19. }
  20. public void setnumber(String number) {
  21. this.number = number;
  22. }
  23. public String getsex() {
  24. return sex ;
  25. }
  26. public void setsex(String sex ) {
  27. this.sex =sex ;
  28. }
  29. public int getage() {
  30.  
  31. return age;
  32. }
  33. public void setage(int age) {
  34.  
  35. this.age= age;
  36. }
  37.  
  38. public String gethometown() {
  39. return hometown;
  40. }
  41. public void sethometown(String hometown) {
  42. this.hometown=hometown ;
  43. }
  44.  
  45. public int compareTo(Person o) {
  46. return this.name.compareTo(o.getName());
  47. }
  48.  
  49. public String toString() {
  50. return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+hometown+"\n";
  51. }
  52. }

    结果:

实验4:内部类语法验证实验

  实验程序1:

    编辑、调试运行教材246页-247页程序6-7,结合程序运行结果理解程序;

    了解内部类的基本用法。

  代码:

  1. package innerClass;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.time.*;
  6.  
  7. import javax.swing.*;
  8.  
  9. /**
  10. * This program demonstrates the use of inner classes.
  11. * @version 1.11 2017-12-14
  12. * @author Cay Horstmann
  13. */
  14. public class InnerClassTest
  15. {
  16. public static void main(String[] args)
  17. {
  18. TalkingClock clock = new TalkingClock(1000, true);//创建TalkingClock对象
  19. clock.start();
  20.  
  21. // keep program running until the user selects "OK"出现一个对话框
  22. JOptionPane.showMessageDialog(null, "Quit program?");
  23. System.exit(0);
  24. }
  25. }
  26.  
  27. /**
  28. * A clock that prints the time in regular intervals.
  29. */
  30. class TalkingClock
  31. {
  32. private int interval;
  33. private boolean beep;
  34.  
  35. /**
  36. * Constructs a talking clock
  37. * @param interval the interval between messages (in milliseconds)
  38. * @param beep true if the clock should beep
  39. */
  40. public TalkingClock(int interval, boolean beep)
  41. {
  42. this.interval = interval;
  43. this.beep = beep;
  44. }
  45.  
  46. /**
  47. * Starts the clock.
  48. */
  49. public void start()
  50. {
  51. TimePrinter listener = new TimePrinter();
  52. Timer timer = new Timer(interval, listener);
  53. timer.start();//启动定时器
  54. }
  55.  
  56. public class TimePrinter implements ActionListener//TimePrinter类实现ActionListener接口
  57. {
  58. public void actionPerformed(ActionEvent event)
  59. {
  60. System.out.println("At the tone, the time is "
  61. + Instant.ofEpochMilli(event.getWhen()));
  62. if (beep) Toolkit.getDefaultToolkit().beep();
  63. }
  64. }
  65. }

   结果:

       实验程序2:

      编辑、调试运行教材254页程序6-8,结合程序运行结果理解程序;

        掌握匿名内部类的用法。

     代码:

  1. package anonymousInnerClass;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.time.*;
  6.  
  7. import javax.swing.*;
  8.  
  9. /**
  10. * This program demonstrates anonymous inner classes.
  11. * @version 1.12 2017-12-14
  12. * @author Cay Horstmann
  13. */
  14. public class AnonymousInnerClassTest
  15. {
  16. public static void main(String[] args)
  17. {
  18. TalkingClock clock = new TalkingClock();
  19. clock.start(1000, true);
  20.  
  21. // keep program running until the user selects "OK"
  22. JOptionPane.showMessageDialog(null, "Quit program?");
  23. System.exit(0);
  24. }
  25. }
  26.  
  27. /**
  28. * A clock that prints the time in regular intervals.
  29. */
  30. class TalkingClock
  31. {
  32. /**
  33. * Starts the clock.
  34. * @param interval the interval between messages (in milliseconds)
  35. * @param beep true if the clock should beep
  36. */
  37. public void start(int interval, boolean beep)
  38. {
  39. ActionListener listener = new ActionListener()//创建一个实现ActionListener接口的类的新对象
  40. {
  41. public void actionPerformed(ActionEvent event)
  42. {
  43. System.out.println("At the tone, the time is "
  44. + Instant.ofEpochMilli(event.getWhen()));
  45. if (beep) Toolkit.getDefaultToolkit().beep();
  46. }
  47. };
  48. Timer timer = new Timer(interval, listener);
  49. timer.start();//启动计时器
  50. }
  51. }

    结果:

  实验程序3:

    在elipse IDE中调试运行教材257页-258页程序6-9,结合程序运行结果理解程序;

    了解静态内部类的用法。

    代码:

  1. package staticInnerClass;
  2.  
  3. /**
  4. * This program demonstrates the use of static inner classes.
  5. * @version 1.02 2015-05-12
  6. * @author Cay Horstmann
  7. */
  8. public class StaticInnerClassTest
  9. {
  10. public static void main(String[] args)
  11. {
  12. double[] values = new double[20];
  13. for (int i = 0; i < values.length; i++)
  14. values[i] = 100 * Math.random();
  15. ArrayAlg.Pair p = ArrayAlg.minmax(values);
  16. System.out.println("min = " + p.getFirst());
  17. System.out.println("max = " + p.getSecond());
  18. }
  19. }
  20.  
  21. class ArrayAlg
  22. {
  23. /**
  24. * A pair of floating-point numbers
  25. */
  26. public static class Pair
  27. {
  28. private double first;
  29. private double second;
  30.  
  31. /**
  32. * Constructs a pair from two floating-point numbers
  33. * @param f the first number
  34. * @param s the second number
  35. */
  36. public Pair(double f, double s)
  37. {
  38. first = f;
  39. second = s;
  40. }
  41.  
  42. /**
  43. * Returns the first number of the pair
  44. * @return the first number
  45. */
  46. public double getFirst()
  47. {
  48. return first;
  49. }
  50.  
  51. /**
  52. * Returns the second number of the pair
  53. * @return the second number
  54. */
  55. public double getSecond()
  56. {
  57. return second;
  58. }
  59. }
  60.  
  61. /**
  62. * Computes both the minimum and the maximum of an array
  63. * @param values an array of floating-point numbers
  64. * @return a pair whose first element is the minimum and whose second element
  65. * is the maximum
  66. */
  67. public static Pair minmax(double[] values)
  68. {
  69. double min = Double.POSITIVE_INFINITY;
  70. double max = Double.NEGATIVE_INFINITY;
  71. for (double v : values)
  72. {
  73. if (min > v) min = v;
  74. if (max < v) max = v;
  75. }
  76. return new Pair(min, max);
  77. }
  78. }

    结果:

实验总结:

  通过本次实验,我学到了:1.接口的定义,使用,实现           2.回调       3.comarator接口     4.对象克隆

5.拷贝     6.lambda表达式     7.内部类

本次实验主要侧重于测试实验,以此来加深对代码的理解,加深对知识的巩固。在实验中主要问题集中编写代码,存在的问题很多。思路不是很清晰,语法未完全掌握,导致错误很多,在查书请教同学后勉强完成,但不明白的地方还很多,一定要在后来的学习中解决这些问题。在知识上对接口和lambda表达式掌握程度不好。在助教线上讲解接口及实现后,对接口有了更深入的理解,明白了继承与接口的区别。但lambda表达式的理解还是较困难,这一章掌握的不太好,在线下我要多花费工夫,多看视频,多动手多练习。

201871010106-丁宣元 《面向对象程序设计(java)》第八周学习总结的更多相关文章

  1. 201771010134杨其菊《面向对象程序设计java》第九周学习总结

                                                                      第九周学习总结 第一部分:理论知识 异常.断言和调试.日志 1.捕获 ...

  2. 201871010132-张潇潇《面向对象程序设计(java)》第一周学习总结

    面向对象程序设计(Java) 博文正文开头 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cn ...

  3. 扎西平措 201571030332《面向对象程序设计 Java 》第一周学习总结

    <面向对象程序设计(java)>第一周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 ...

  4. 杨其菊201771010134《面向对象程序设计Java》第二周学习总结

    第三章 Java基本程序设计结构 第一部分:(理论知识部分) 本章主要学习:基本内容:数据类型:变量:运算符:类型转换,字符串,输入输出,控制流程,大数值以及数组. 1.基本概念: 1)标识符:由字母 ...

  5. 201871010124 王生涛《面向对象程序设计JAVA》第一周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://edu.cnblogs.com/campus/xbsf/ ...

  6. 201871010115——马北《面向对象程序设计JAVA》第二周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  7. 201777010217-金云馨《面向对象程序设计(Java)》第二周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  8. 201871010132——张潇潇《面向对象程序设计JAVA》第二周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  9. 201771010123汪慧和《面向对象程序设计Java》第二周学习总结

    一.理论知识部分 1.标识符由字母.下划线.美元符号和数字组成, 且第一个符号不能为数字.标识符可用作: 类名.变量名.方法名.数组名.文件名等.第二部分:理论知识学习部分 2.关键字就是Java语言 ...

  10. 20155321 2016-2017-2 《Java程序设计》第八周学习总结

    20155321 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 创建Logger对象 static Logger getLogger(String name ...

随机推荐

  1. C++ STL priority_queue 优先队列

    优先队列: 与队列的用法是一样的,优先队列内部是通过堆来排序实现的, #include<iostream> #include <queue> using namespace s ...

  2. 28道java基础面试题-下

    28道java基础面试题下 15.Java语言如何进行异常处理,关键字:throws.throw.try.catch.finally分别如何使用? 答:Java通过面向对象的方法进行异常处理,把各种不 ...

  3. (day55)七、查询优化、MTV和MCV、choices、AJAX、序列化

    目录 一.ORM查询优化 (一)only与defer (1)only (2)defer (二)select_related与prefatch_related (1)select_related (2) ...

  4. Windows开机自动登录账户

    如何在Windows设了账户密码的情况下开机自动登录账户,有以下两种方法. 通过Windows设置自动登录 按“Win+R”组合键打开“运行”框内输入“netplwiz”. 打开以下窗口,将“要使用本 ...

  5. win10 + 3ds Max 2014 问题记录

    3ds Max 下载: https://zixue.3d66.com/popsoft_201.html VRay 下载: https://zixue.3d66.com/softhtml/showsof ...

  6. MySQL实战45讲学习笔记:第四十五讲

    一.本节概述 MySQL 里有很多自增的 id,每个自增 id 都是定义了初始值,然后不停地往上加步长.虽然自然数是没有上限的,但是在计算机里,只要定义了表示这个数的字节长度,那它就有上限.比如,无符 ...

  7. MySQL实战45讲学习笔记:第三十六讲

    一.引子 今天是大年三十,在开始我们今天的学习之前,我要先和你道一声春节快乐! 在上一篇文章中,我们在优化 join 查询的时候使用到了临时表.当时,我们是这么用的: create temporary ...

  8. 无聊系列 - C#中一些常用类型与java的类型对应关系

    昨天在那个.NET转java群里,看到一位朋友在问C#的int 对应java的哪个对象,就心血来潮,打算写一下C#中一些基础性的东西,在java中怎么找. 1. 基础值类型 如:int,long,do ...

  9. 解决 Github 图片加载慢的问题

    一.前言 本文主要介绍一种解决 Github 图片加载慢的方法,亲测有效. 笔者博客是使用 Github 作为图床,每次打开博客时的图片加载很慢或者根本加载不出来.这是因为 GitHub 的 CDN ...

  10. Shell基本运算符之字符串运算符

    Shell基本运算符 1.字符串运算符 常用的字符串运算符 运算符 说明 例子 = 检测两字符串是否相等,相等返回true [ $a = $b ] != 检测两个字符串是否部相等,不相等返回true ...