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

时间:2022-06-03 09:33:49

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

package interfaces;

public class Employee implements Comparable<Employee>//Employee类实现Comparable接口,并为泛型Comparable接口提供一个类型参数
{
private String name;
private double salary; public Employee(String name, double salary)//构造器
{
this.name = name;
this.salary = salary;
} public String getName()//访问器
{
return name;
} public double getSalary()//访问器
{
return salary;
} public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
} /**
* Compares employees by salary
* @param other another Employee object
* @return a negative value if this employee has a lower salary than
* otherObject, 0 if the salaries are the same, a positive value otherwise
*/
public int compareTo(Employee other)//Employee类的compareTo方法
{
return Double.compare(salary, other.salary);//Double的compare方法,比较对象是salary
}
}

EmployeeSortTest.java

package interfaces;

import java.util.*;

/**
* This program demonstrates the use of the Comparable interface.
* @version 1.30 2004-02-27
* @author Cay Horstmann
*/
public class EmployeeSortTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];//创建数组 staff[0] = new Employee("Harry Hacker", 35000);
staff[1] = new Employee("Carl Cracker", 75000);
staff[2] = new Employee("Tony Tester", 38000); Arrays.sort(staff);//调用Arrays类的sort方法进行排序,Employee要实现Comparable接口 // print out information about all Employee objects
for (Employee e : staff)//foreach循环遍历打印输出信息
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
}
}

结果:

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

测试程序2:

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

代码:

interface  A  //A是一个接口
{
double g=9.8;
void show( );
}
class C implements A //C类实现A接口
{
public void show( )
{System.out.println("g="+g);}
} class InterfaceTest
{
public static void main(String[ ] args)
{
A a=new C( );
a.show( );
System.out.println("g="+C.g);//用C调用A中的变量
}
}

结果:

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

测试程序3:

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

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

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

  掌握回调程序设计模式;

代码:

package timer;

/**
@version 1.02 2017-12-14
@author Cay Horstmann
*/ import java.awt.*;
import java.awt.event.*;
import java.time.*;
import javax.swing.*; public class TimerTest
{
public static void main(String[] args)
{
ActionListener listener = new TimePrinter(); // construct a timer that calls the listener创建listener的timer数组
// once every second
Timer timer = new Timer(10000, listener);//创建定时器类对象timer
timer.start();//启动定时器 // keep program running until the user selects "OK"
JOptionPane.showMessageDialog(null, "Quit program?");//启动程序后,会弹出"Quit program?"
System.exit(0);
}
} class TimePrinter implements ActionListener//TimePrinter类实现ActionListener接口
{
public void actionPerformed(ActionEvent event)
{
System.out.println("At the tone, the time is " //每隔10秒,信息输出一次,响一声铃
+ Instant.ofEpochMilli(event.getWhen()));
Toolkit.getDefaultToolkit().beep();//调用getDefaultToolkit()方法
}
}

结果:

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

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

测试程序4:

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

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

     掌握对象克隆实现技术;

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

代码:

CloneTest.java

package clone;

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

Employee.java

package clone;

import java.util.Date;
import java.util.GregorianCalendar; public class Employee implements Cloneable
{
private String name;
private double salary;
private Date hireDay; public Employee(String name, double salary)//Employee构造器
{
this.name = name;
this.salary = salary;
hireDay = new Date();
} public Employee clone() throws CloneNotSupportedException//声明异常,在一个对象上调用clone,但没有实现Cloneable的接口,Object类的clone方法就会throw出CloneNotSupportedException
{
// call Object.clone()调用对象克隆
Employee cloned = (Employee) super.clone(); // clone mutable fields
cloned.hireDay = (Date) hireDay.clone(); return cloned;
} /**
* Set the hire day to a given date.
* @param year the year of the hire day
* @param month the month of the hire day
* @param day the day of the hire day
*/
public void setHireDay(int year, int month, int day)
{
Date newHireDay = new GregorianCalendar(year, month - 1, day).getTime(); // example of instance field mutation实例字段突变
hireDay.setTime(newHireDay.getTime());
} public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
} public String toString()
{
return "Employee[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
}
}

结果:

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

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

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

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

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

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

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

代码:

package lambda;

import java.util.*;

import javax.swing.*;
import javax.swing.Timer; /**
* This program demonstrates the use of lambda expressions.
* @version 1.0 2015-05-12
* @author Cay Horstmann
*/
public class LambdaTest
{
public static void main(String[] args)
{
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune" };
System.out.println(Arrays.toString(planets)); //调用Arrays的toString方法,并将planets输出在控制台上
System.out.println("Sorted in dictionary order:");
Arrays.sort(planets);//排序
System.out.println(Arrays.toString(planets));
System.out.println("Sorted by length:");
Arrays.sort(planets, (first, second) -> first.length() - second.length());//推导出first,second的类型
System.out.println(Arrays.toString(planets)); Timer timer = new Timer(1000, event ->
System.out.println("The time is " + new Date()));
timer.start(); //启动计时器 // keep program running until user selects "OK" 运行直至点击OK
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}

结果:

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

注:lambda表达式的优缺点:

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

    缺:难理解

 实验3: 编程练习

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

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

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

  查询最小年龄人员信息;

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

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

代码:

    Identify.java

package IdentifySearch;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner; public class Identify {
private static ArrayList<Person> personlist; public static void main(String[] args) {
personlist = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
File file = new File("E:\\身份证号.txt");
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String temp = null;
while ((temp = in.readLine()) != null) { Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" ");
String name = linescanner.next();
String number = linescanner.next();
String sex = linescanner.next();
String age = linescanner.next();
String hometown = linescanner.nextLine();
Person person = new Person();
person.setName(name);
person.setnumber(number);
person.setsex(sex);
int A = Integer.parseInt(age);
person.setage(A);
person.sethometown(hometown);
personlist.add(person); }
} catch (FileNotFoundException e) {
System.out.println("身份信息文件找不到");
e.printStackTrace();
} catch (IOException e) {
System.out.println("身份信息文件读取错误");
e.printStackTrace();
}
boolean isTrue = true;
while (isTrue) {
System.out.println("0.按姓名字典序输出人员信息;");
System.out.println("1.查询最大年龄人员信息;;");
System.out.println("2.查询最小年龄人员信息;");
System.out.println("3.寻找同乡;");
System.out.println("4.寻找年龄相近的人;");
System.out.println("5.退出。");
String W = scanner.next();
switch (W) {
case "0":
Collections.sort(personlist);
System.out.println(personlist.toString());
break;
case "1":
int a = 0;
int j, c1 = 0, d1 = 0;
for (int i = 1; i < personlist.size(); i++) {
j = personlist.get(i).getage();
if (j > a) {
a = j;
c1 = i;
}
}
System.out.println("年龄最大:" + personlist.get(c1));
break;
case "2":
int b = 100;
int c2 = 0,d2 = 0;
for (int i = 1; i < personlist.size(); i++) {
j = personlist.get(i).getage();
if (j < b) {
b = j;
d2 = i;
}
}
System.out.println("年龄最小:" + personlist.get(d2));
break;
case "3":
System.out.println("籍贯:");
String search = scanner.next();
String place = search.substring(0, 3);
int i = 0;
for (; i < personlist.size(); i++) {
if (personlist.get(i).gethometown().substring(1, 4).equals(place))
System.out.println("你的同乡是:" + personlist.get(i));
}
break;
case "4":
System.out.println("年龄:");
int yourage = scanner.nextInt();
int nearaga = agenear(yourage);
int value = yourage - personlist.get(nearaga).getage();
System.out.println("" + personlist.get(nearaga));
break;
case "5":
isTrue = false;
System.out.println("退出程序!");
break;
default:
System.out.println("检查输入!");
}
}
} public static int agenear(int age) {
int j = 0, b = 53, value = 0, c = 0;
for (int i = 0; i < personlist.size(); i++) {
value = personlist.get(i).getage() - age;
if (value < 0)
value = -value;
if (value < b) {
b = value;
c = i;
}
}
return c;
}
}

    Person.java

package IdentifySearch;

public class Person implements Comparable<Person> {

    private String name;
private String number ;
private String sex ;
private int age;
private String hometown; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getnumber() {
return number;
}
public void setnumber(String number) {
this.number = number;
}
public String getsex() {
return sex ;
}
public void setsex(String sex ) {
this.sex =sex ;
}
public int getage() { return age;
}
public void setage(int age) { this.age= age;
} public String gethometown() {
return hometown;
}
public void sethometown(String hometown) {
this.hometown=hometown ;
} public int compareTo(Person o) {
return this.name.compareTo(o.getName());
} public String toString() {
return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+hometown+"\n";
}
}

    结果:

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

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

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

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

  实验程序1:

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

    了解内部类的基本用法。

  代码:

package innerClass;

import java.awt.*;
import java.awt.event.*;
import java.time.*; import javax.swing.*; /**
* This program demonstrates the use of inner classes.
* @version 1.11 2017-12-14
* @author Cay Horstmann
*/
public class InnerClassTest
{
public static void main(String[] args)
{
TalkingClock clock = new TalkingClock(1000, true);//创建TalkingClock对象
clock.start(); // keep program running until the user selects "OK"出现一个对话框
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
} /**
* A clock that prints the time in regular intervals.
*/
class TalkingClock
{
private int interval;
private boolean beep; /**
* Constructs a talking clock
* @param interval the interval between messages (in milliseconds)
* @param beep true if the clock should beep
*/
public TalkingClock(int interval, boolean beep)
{
this.interval = interval;
this.beep = beep;
} /**
* Starts the clock.
*/
public void start()
{
TimePrinter listener = new TimePrinter();
Timer timer = new Timer(interval, listener);
timer.start();//启动定时器
} public class TimePrinter implements ActionListener//TimePrinter类实现ActionListener接口
{
public void actionPerformed(ActionEvent event)
{
System.out.println("At the tone, the time is "
+ Instant.ofEpochMilli(event.getWhen()));
if (beep) Toolkit.getDefaultToolkit().beep();
}
}
}

   结果:

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

       实验程序2:

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

        掌握匿名内部类的用法。

     代码:

package anonymousInnerClass;

import java.awt.*;
import java.awt.event.*;
import java.time.*; import javax.swing.*; /**
* This program demonstrates anonymous inner classes.
* @version 1.12 2017-12-14
* @author Cay Horstmann
*/
public class AnonymousInnerClassTest
{
public static void main(String[] args)
{
TalkingClock clock = new TalkingClock();
clock.start(1000, true); // keep program running until the user selects "OK"
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
} /**
* A clock that prints the time in regular intervals.
*/
class TalkingClock
{
/**
* Starts the clock.
* @param interval the interval between messages (in milliseconds)
* @param beep true if the clock should beep
*/
public void start(int interval, boolean beep)
{
ActionListener listener = new ActionListener()//创建一个实现ActionListener接口的类的新对象
{
public void actionPerformed(ActionEvent event)
{
System.out.println("At the tone, the time is "
+ Instant.ofEpochMilli(event.getWhen()));
if (beep) Toolkit.getDefaultToolkit().beep();
}
};
Timer timer = new Timer(interval, listener);
timer.start();//启动计时器
}
}

    结果:

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

  实验程序3:

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

    了解静态内部类的用法。

    代码:

package staticInnerClass;

/**
* This program demonstrates the use of static inner classes.
* @version 1.02 2015-05-12
* @author Cay Horstmann
*/
public class StaticInnerClassTest
{
public static void main(String[] args)
{
double[] values = new double[20];
for (int i = 0; i < values.length; i++)
values[i] = 100 * Math.random();
ArrayAlg.Pair p = ArrayAlg.minmax(values);
System.out.println("min = " + p.getFirst());
System.out.println("max = " + p.getSecond());
}
} class ArrayAlg
{
/**
* A pair of floating-point numbers
*/
public static class Pair
{
private double first;
private double second; /**
* Constructs a pair from two floating-point numbers
* @param f the first number
* @param s the second number
*/
public Pair(double f, double s)
{
first = f;
second = s;
} /**
* Returns the first number of the pair
* @return the first number
*/
public double getFirst()
{
return first;
} /**
* Returns the second number of the pair
* @return the second number
*/
public double getSecond()
{
return second;
}
} /**
* Computes both the minimum and the maximum of an array
* @param values an array of floating-point numbers
* @return a pair whose first element is the minimum and whose second element
* is the maximum
*/
public static Pair minmax(double[] values)
{
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
for (double v : values)
{
if (min > v) min = v;
if (max < v) max = v;
}
return new Pair(min, max);
}
}

    结果:

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

实验总结:

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

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

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