实验六 接口的定义与使用
一、理论部分
该章学习的主要内容便有接口,回调,对象克隆,lambda表达式,内部类以及代理
1、接口:
(1)Java为了克服单继承的缺点,Java使用了接口,一个类可以实现一个或多个接口
(2)在Java程序设计语言中,接口不是类,而是对类的一组需求描述,由常量和一组抽象方法组成。接口中不包括变量和有具体实现的方法
(3)只要类实现了接口,则该类要遵从接口描述的统一格式进行定义,并且可以在任何需要该接口的地方使用这个类的对象
(4)类似建立类的继承关系,接口也可以扩展
扩展方法:public interface 接口1 extends 接口2
{ ...... }
使用extends来继承接口的常量和抽象方法,扩展形成新的接口
接口中的所有常量必须是public static final,方法必须是public abstract
(5)在类声明时用implements关键字声明使用一个或多个接口;一个类使用了某个接口,那么这个类必须实现该接口的所有方法,即为这些方法提供方法体;一个类可以实现多个接口,接口间应该用逗号分隔开
(6)接口不能构造接口对象,但可以声明接口变量以指向一个实现了该接口的类对象
(7)接口与抽象类的区别:
(a)接口不能实现任何方法,而抽象类可以
(b)类可以实现许多接口,但只有一个父类
(c)接口不是类分级结构的一部分,无任何联系的类可以实现相同的接口
(8)示例:Arrays类中的sort方法承诺可以对对象数组进行排序,但要求满足下列前提:对象所属的类必须实现了Comparable接口
任何实现了Comparable接口的类都需要包含compareTo方法
2.回调:
(1)回调:一种程序设计模式,在这种模式中,可指出某个特定事件发生时程序应该采用的动作
(2)在java.swing包中有一个Timer类,可以使用它在到达给定的时间间隔时触发一个事件
—Timer(int interval,ActionListener listener)
—void start()
—void stop()
3.Comparator接口:
(1)所在包:java.util.*
(2)用途:处理字符串按长度进行排序的操作
4.对象克隆:
(1)拷贝:当拷贝一个对象变量时,原始变量与拷贝变量引用同一个对象。这样,改变一个变量所引用的对象会对另一个变量产生影响
(2)克隆:如果要创建一个对象新的copy,它的初始状态与original相同,但以后可以各自改变状态,就需要使用Object类的clone方法
(3)浅层拷贝:被拷贝对象的所有常量成员和基本类型属性都有与原来对象相同的拷贝值,而若成员域是一个对象,则被拷贝对象该对象域的对象引用任然指向原来的对象
(4)深层拷贝:被拷贝对象的所有成员域都含有与原来对象相同的值,且对象域将指向被复制过的新对象,而不是原有对象被引用的对象。换言之,深层拷贝将拷贝对象内引用的对象也拷贝一遍
(5)在子类中实现Cloneable接口;
为了获取对象的一份拷贝,使用Object类的clone方法;
在类中覆盖超类的clone方法,声明为public;
在类的clone方法中,调用super.clone()
5.lambda表达式:
(1)用途:提供一 个函数化的语法来简化编码
(2)函数式接口 Functionallnterface
(3)创建函数式接口:有时自定义一个函数式接口,做法也很简单,首先此接口只能有一个函数操作,然后在接口类型上标注注解@Functionallnterface即可
6. 内部类:
(1)内部类(inner class)是定义在一个类内部的类;外层的类成为外部类(outer class)
(2)使用内部类的原因有以下三个:
—内部类方法可以访问该类定义所在的作用域中的数据,包括私有数据
—内部类可以隐藏起来,不为同一包中的其他类所见
—想要定义一个回调函数且不想编写大量代码时使用匿名内部类比较便捷
(3)内部类可以直接访问外部类的成员,包括private成员,但是内部类的成员却不能被外部类直接访问
(4)内部类并非只能在类内定义,也可以在程序块内定义局部内部类;局部内部类不能用public或private访问修饰符进行声明,它的作用域被限定在声明这个局部类的块中
(5)局部内部类:可以访问方法中的final类型的局部变量
(6)匿名内部类:只创建类的一个对象,没有类名
(7)静态内部类:用static修饰一个内部类,这个类就相当于是一个外部定义的类,所以static的内部类中可以声明static成员
7.代理(了解):
代理可在程序运行时创建一个实现了一组给定接口的新类,可分为:静态代理和动态代理
二.实验部分
1.实验目的与要求
(1) 掌握接口定义方法;
(2) 掌握实现接口类的定义要求;
(3) 掌握实现了接口类的使用要求;
(4) 掌握程序回调设计模式;
(5) 掌握Comparator接口用法;
(6) 掌握对象浅层拷贝与深层拷贝方法;
(7) 掌握Lambda表达式语法;
(8) 了解内部类的用途及语法要求。
实验1: 导入第6章示例程序,测试程序并进行代码注释。
测试程序1:
l 编辑、编译、调试运行阅读教材214页-215页程序6-1、6-2,理解程序并分析程序运行结果;
l 在程序中相关代码处添加新知识的注释。
l 掌握接口的实现用法;
l 掌握内置接口Compareable的用法。
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);//静态方法sort // print out information about all Employee objects for (Employee e : staff) System.out.println("name=" + e.getName() + ",salary=" + e.getSalary()); } }
package interfaces;
public class Employee implements Comparable<Employee>//Employee实现JDK内置接口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) { return Double.compare(salary, other.salary);//静态Double.compare方法 } }
测试程序2:
l 编辑、编译、调试以下程序,结合程序运行结果理解程序;
interface A { double g=9.8; void show( ); } class C implements 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); } } |
public interface A
{
double g=9.8; void show( ); }
class C implements A
{
public void show( )
{System.out.println("g="+g);} }
package InterfaceTest;
public class InterfaceTest {
public static void main(String[ ] args) { A a=new C( ); a.show( ); System.out.println("g="+C.g); } }
测试程序3:
l 在elipse IDE中调试运行教材223页6-3,结合程序运行结果理解程序;
l 26行、36行代码参阅224页,详细内容涉及教材12章。
l 在程序中相关代码处添加新知识的注释。
l 掌握回调程序设计模式。
package timer;
/**
@version 1.01 2015-05-12
@author Cay Horstmann
*/
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer; // to resolve conflict with java.util.Timer public class TimerTest { public static void main(String[] args) { ActionListener listener = new TimePrinter(); //实现了ActionListener的类对象 // construct a timer that calls the listener // once every 10 seconds Timer t = new Timer(10000, listener);//定义间隔 t.start(); JOptionPane.showMessageDialog(null, "Quit program?"); System.exit(0); } } class TimePrinter implements ActionListener//内置接口 { public void actionPerformed(ActionEvent event) { System.out.println("At the tone, the time is " + new Date()); Toolkit.getDefaultToolkit().beep(); } }
测试程序4:
l 调试运行教材229页-231页程序6-4、6-5,结合程序运行结果理解程序;
l 在程序中相关代码处添加新知识的注释。
l 掌握对象克隆实现技术;
l 掌握浅拷贝和深拷贝的差别。
package clone;
/**
* This program demonstrates cloning.
* @version 1.10 2002-07-01
* @author Cay Horstmann
*/
public class CloneTest { public static void main(String[] args) { try { Employee original = new Employee("John Q. Public", 50000); //Employee是一个自定义类 original.setHireDay(2000, 1, 1); Employee copy = original.clone(); copy.raiseSalary(10);//原有对象不会发生变化 copy.setHireDay(2002, 12, 31);//更改器 System.out.println("original=" + original);//字符串连接 System.out.println("copy=" + copy); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }
实验2: 导入第6章示例程序6-6,学习Lambda表达式用法。
l 调试运行教材233页-234页程序6-6,结合程序运行结果理解程序;
l 在程序中相关代码处添加新知识的注释。
l 将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" };//定义数组planets System.out.println(Arrays.toString(planets));//静态方法 System.out.println("Sorted in dictionary order:"); Arrays.sort(planets);//Arrays.sort方法接收实验Lambda类的对象 System.out.println(Arrays.toString(planets)); System.out.println("Sorted by length:"); Arrays.sort(planets, (first, second) -> first.length() - second.length());//Lambda表达式 System.out.println(Arrays.toString(planets)); Timer t = new Timer(1000, event -> System.out.println("The time is " + new Date()));//Lambda表达式 t.start(); // keep program running until user selects "Ok" JOptionPane.showMessageDialog(null, "Quit program?"); System.exit(0); //返回类型 } }
package clone;
import java.util.Date;
import java.util.GregorianCalendar;
public class Employee implements Cloneable
{
//定义三个私有属性 private String name;//string类在lang包 private double salary; private Date hireDay; public Employee(String name, double salary) { this.name = name; this.salary = salary; hireDay = new Date(); }//构造方法 public Employee clone() throws 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 + "]"; } }
注:以下实验课后完成
实验3: 编程练习
l 编制一个程序,将身份证号.txt 中的信息读入到内存中;
l 按姓名字典序输出人员信息;
l 查询最大年龄的人员信息;
l 查询最小年龄人员信息;
l 输入你的年龄,查询身份证号.txt中年龄与你最近人的姓名、身份证号、年龄、性别和出生地;
l 查询人员中是否有你的同乡。
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 Search{
private static ArrayList<Person> Personlist1; public static void main(String[] args) { Personlist1 = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("E:\\面向对象程序设计Java\\实验\\实验六\\身份证号.txt"); try { FileInputStream F = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(F)); String temp = null; while ((temp = in.readLine()) != null) { Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" "); String name = linescanner.next(); String id = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String place =linescanner.nextLine(); Person Person = new Person(); Person.setname(name); Person.setid(id); Person.setsex(sex); int a = Integer.parseInt(age); Person.setage(a); Person.setbirthplace(place); Personlist1.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("******************************************"); System.out.println("1:按姓名字典顺序输出信息;"); System.out.println("2:查询最大年龄与最小年龄人员信息;"); System.out.println("3:按省份找你的同乡;"); System.out.println("4:输入你的年龄,查询年龄与你最近人的信息;"); System.out.println("5:退出"); System.out.println("******************************************"); int type = scanner.nextInt(); switch (type) { case 1: Collections.sort(Personlist1); System.out.println(Personlist1.toString()); break; case 2: int max=0,min=100;int j,k1 = 0,k2=0; for(int i=1;i<Personlist1.size();i++) { j=Personlist1.get(i).getage(); if(j>max) { max=j; k1=i; } if(j<min) { min=j; k2=i; } } System.out.println("年龄最大:"+Personlist1.get(k1)); System.out.println("年龄最小:"+Personlist1.get(k2)); break; case 3: System.out.println("place?"); String find = scanner.next(); String place=find.substring(0,3); String place2=find.substring(0,3); for (int i = 0; i <Personlist1.size(); i++) { if(Personlist1.get(i).getbirthplace().substring(1,4).equals(place)) { System.out.println("你的同乡:"+Personlist1.get(i)); } } break; case 4: System.out.println("年龄:"); int yourage = scanner.nextInt(); int close=ageclose(yourage); int d_value=yourage-Personlist1.get(close).getage(); System.out.println(""+Personlist1.get(close)); break; case 5: isTrue = false; System.out.println("再见!"); break; default: System.out.println("输入有误"); } } } public static int ageclose(int age) { int m=0; int max=53; int d_value=0; int k=0; for (int i = 0; i < Personlist1.size(); i++) { d_value=Personlist1.