1、實驗目的與要求
(1) 掌握接口定義方法;
(2) 掌握實現接口類的定義要求;
(3) 掌握實現了接口類的使用要求;
(4) 掌握程序回調設計模式;
(5) 掌握Comparator接口用法;
(6) 掌握對象淺層拷貝與深層拷貝方法;
(7) 掌握Lambda表達式語法;
(8) 了解內部類的用途及語法要求。
2、實驗內容和步驟
實驗1:?導入第6章示例程序,測試程序并進行代碼注釋。
測試程序1:
編輯、編譯、調試運行閱讀教材214頁-215頁程序6-1、6-2,理解程序并分析程序運行結果;
在程序中相關代碼處添加新知識的注釋。
掌握接口的實現用法;
掌握內置接口Compareable的用法。
?
package interfaces;import java.util.*;/*** 這個程序演示了可比接口的使用。* @version 1.30 2004-02-27* @author Cay Horstmann*/ public class EmployeeSortTest {public static void main(String[] args){//new 新建一個Employee 數組對象 給staff所引用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);//對staff進行排序的方法 Arrays.sort(staff);// 打印所有員工對象的信息for (Employee e : staff)System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());} }
?
package interfaces;//將Employee類聲明為實現Comparable接口 public class Employee implements Comparable<Employee> {private String name;//某個類定義了一個只能在該類內部才能訪問的,名叫name的字符串變量private double salary;//某個類定義了一個只能在該類內部才能訪問的,名叫salary的字符串變量//利用構造方法初始化各個域public Employee(String name, double salary){this.name = name;this.salary = salary;}//Name訪問器 public String getName(){return name;}//Salary訪問器public double getSalary(){return salary;}//調用方法,改寫工資數據public void raiseSalary(double byPercent){double raise = salary * byPercent / 100;salary += raise;}/*** 用工資比較員工* @一個雇員對象參數 * @如果雇員工資較低,則返回負值* otherObject, 0 if the salaries are the same, a positive value otherwise * 如果工資相同,則以正值計算*///用這個對象與other進行比較。如果這個對象小于other,則返回負值;如果相等則返回0;否則返回正值public int compareTo(Employee other){return Double.compare(salary, other.salary);} }
測試程序2:
編輯、編譯、調試以下程序,
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); ??} } |
?
//創建一個接口A interface A {//給g賦值一個雙精度型值double g=9.8;//一個用戶自定義函數名,完成輸出操作void show( ); }
//C是類名,implements是實現A與java之間的接口
class C implements A
{//展示信息public void show( ) //輸出g的值 {System.out.println("g="+g);} }
?
class InterfaceTest
{ public static void main(String[ ] args){//C是A的子類,實例化一個子類對象aA a=new C( ); //顯示子類對象a a.show( ); //輸出C中輸出的結果 System.out.println("g="+C.g); } }
測試程序3:
在elipse IDE中調試運行教材223頁6-3,結合程序運行結果理解程序;
26行、36行代碼參閱224頁,詳細內容涉及教材12章。
在程序中相關代碼處添加新知識的注釋。
掌握回調程序設計模式;
?
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; // 用JavaUTIL計時器解決沖突 public class TimerTest { public static void main(String[] args){ //TimePrinter實現了ActionListerActionListener listener = new TimePrinter();//構建一個調用偵聽器的計時器// 每10秒一次 Timer t = new Timer(10000, listener);//t對象調用start方法 t.start();//顯示包含一條消息和OK按鈕的對話框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:
?調試運行教材229頁-231頁程序6-4、6-5,結合程序運行結果理解程序;
在程序中相關代碼處添加新知識的注釋。
掌握對象克隆實現技術;
掌握淺拷貝和深拷貝的差別。
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){this.name = name;this.salary = salary;hireDay = new Date();}
//定義一個克隆方法public Employee clone() throws CloneNotSupportedException{// call Object.clone()//任何Object都是可以執行克隆(clone)方法的Employee cloned = (Employee) super.clone();
// 繼承object類// 克隆可變字段 cloned.hireDay = (Date) hireDay.clone();//string沒有克隆功能return cloned;}/*** 把雇傭日定在指定的日期* @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();// 實例字段突變示例 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 + "]";} }
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//放入try,配合后面的語句捕獲異常 {Employee original = new Employee("John Q. Public", 50000);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表達式用法。
調試運行教材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.* //這個程序演示了lambda表達式的用法。* @version 1.0 2015-05-12* @author Cay Horstmann*/ public class LambdaTest {public static void main(String[] args){//初始化一個數組String[] planetsString[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars","Jupiter", "Saturn", "Uranus", "Neptune" };System.out.println(Arrays.toString(planets));System.out.println("Sorted in dictionary order:");Arrays.sort(planets);System.out.println(Arrays.toString(planets));//打印出Arrays.toString方法System.out.println("Sorted by length:");Arrays.sort(planets, (first, second) -> first.length() - second.length());System.out.println(Arrays.toString(planets));Timer t = new Timer(1000, event ->System.out.println("The time is " + new Date()));t.start(); //保持程序運行直到用戶選擇“OK”JOptionPane.showMessageDialog(null, "Quit program?");System.exit(0); }}
?
?
}注:以下實驗課后完成
實驗3:?編程練習
編制一個程序,將身份證號.txt 中的信息讀入到內存中;
按姓名字典序輸出人員信息;
查詢最大年齡的人員信息;
查詢最小年齡人員信息;
輸入你的年齡,查詢身份證號.txt中年齡與你最近人的姓名、身份證號、年齡、性別和出生地;
查詢人員中是否有你的同鄉。
?
public class Student implements Comparable<Student> {private String name;private String number ;private String sex ;private int age;private String province;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) {// int a = Integer.parseInt(age);this.age= age;}public String getprovince() {return province;}public void setprovince(String province) {this.province=province ;}public int compareTo(Student o) {return this.name.compareTo(o.getName());}public String toString() {return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n";} }
?
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 Test {private static ArrayList<Student> studentlist;public static void main(String[] args) {studentlist = new ArrayList<>();Scanner scanner = new Scanner(System.in);File file = new File("F:\\java\\身份證號.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 province =linescanner.nextLine();Student student = new Student();student.setName(name);student.setnumber(number);student.setsex(sex);int a = Integer.parseInt(age);student.setage(a);student.setprovince(province);studentlist.add(student);}} 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.退出 ");String m = scanner.next();switch (m) {case "1":Collections.sort(studentlist); System.out.println(studentlist.toString());break;case "2":int max=0,min=100;int j,k1 = 0,k2=0;for(int i=1;i<studentlist.size();i++){j=studentlist.get(i).getage();if(j>max){max=j; k1=i;}if(j<min){min=j; k2=i;}} System.out.println("年齡最大:"+studentlist.get(k1));System.out.println("年齡最小:"+studentlist.get(k2));break;case "3":System.out.println("地址?");String find = scanner.next(); String place=find.substring(0,3);for (int i = 0; i <studentlist.size(); i++) {if(studentlist.get(i).getprovince().substring(1,4).equals(place)) System.out.println("同鄉"+studentlist.get(i));} break;case "4":System.out.println("年齡:");int yourage = scanner.nextInt();int near=agenear(yourage);int value=yourage-studentlist.get(near).getage();System.out.println(""+studentlist.get(near));break;case "5 ":isTrue = false;System.out.println("退出程序!");break;default:System.out.println("輸入有誤");}}}public static int agenear(int age) { int j=0,min=53,value=0,ok=0;for (int i = 0; i < studentlist.size(); i++){value=studentlist.get(i).getage()-age;if(value<0) value=-value; if (value<min) {min=value;ok=i;} } return ok; }}
?
實驗4:內部類語法驗證實驗
實驗程序1:
編輯、調試運行教材246頁-247頁程序6-7,結合程序運行結果理解程序;
了解內部類的基本用法。
package innerClass;import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer; /*** 這個程序演示了內部類的使用。 * @version 1.11 2015-05-12* @author Cay Horstmann*/ public class InnerClassTest {public static void main(String[] args){TalkingClock clock = new TalkingClock(1000, true);clock.start();//保持程序運行直到用戶選擇“OK” JOptionPane.showMessageDialog(null, "Quit program?");System.exit(0);} }/*** 按規定時間打印時間的鐘*/ class TalkingClock {private int interval;private boolean beep;/*** 構造一個會說話的鐘 * PARAM間隔消息間的間隔(毫秒)* 如果時鐘應該發出嘟嘟聲 */public TalkingClock(int interval, boolean beep){this.interval = interval;this.beep = beep;}/*** Starts the clock. //啟動時鐘 */public void start(){ActionListener listener = new TimePrinter();Timer t = new Timer(interval, listener);t.start();}public class TimePrinter implements ActionListener{public void actionPerformed(ActionEvent event){System.out.println("At the tone, the time is " + new Date());if (beep) Toolkit.getDefaultToolkit().beep();}} }
?
實驗程序2:
編輯、調試運行教材254頁程序6-8,結合程序運行結果理解程序;
了解匿名內部類的用法。
?
package anonymousInnerClass;import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer;/***這個程序演示匿名內部類。 * @version 1.11 2015-05-12* @author Cay Horstmann*/ public class AnonymousInnerClassTest {public static void main(String[] args){TalkingClock clock = new TalkingClock();clock.start(1000, true);// keep program running until user selects "Ok"JOptionPane.showMessageDialog(null, "Quit program?");System.exit(0);} }/***按規定時間打印時間的鐘 */ class TalkingClock {/*** Starts the clock. //啟動時鐘 * @param interval the interval between messages (in milliseconds) //PARAM間隔消息間的間隔(毫秒) * @param beep true if the clock should beep //如果時鐘應該發出嘟嘟聲 */public void start(int interval, boolean beep){ActionListener listener = new ActionListener(){public void actionPerformed(ActionEvent event){System.out.println("At the tone, the time is " + new Date());if (beep) Toolkit.getDefaultToolkit().beep();}};Timer t = new Timer(interval, listener);t.start();} }
?
?
實驗程序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[] d = new double[20];for (int i = 0; i < d.length; i++)d[i] = 100 * Math.random();ArrayAlg.Pair p = ArrayAlg.minmax(d);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 //PARAM值的浮點數數組 * @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);} }
?
總結:
?一、接口:
?Java為了克服單繼承的缺點,Java使用了接口, 一個類可以實現一個或多個接口。 ?
?? 在Java程序設計語言中,接口不是類,而是對類 的一組需求描述,由常量和一組抽象方法組成。 ?接口中不包括變量和有具體實現的方法。 ?
?? 只要類實現了接口,則該類要遵從接口描述的統 一格式進行定義,并且可以在任何需要該接口的 地方使用這個類的對象。
?? 類似建立類的繼承關系,接口也可以擴展。 ?
?? 接口的擴展技術使得從具有較高通用性的接口存在多條鏈延伸到具有較高專用性的接口。
?(1)通常接口的名字以able或ible結尾;
?? (2)可以使用extends來繼承接口的常量和抽象方 法,擴展形成新的接口;
?(3)接口中的所有常量必須是public static final,方法必須是public abstract,這是 系統默認的,不管你在定義接口時,寫不寫 修飾符都是一樣的。
接口與抽象類的區別:
(1)接口不能實現任何方法,而抽象類可以。
(2)類可以實現許多接口,但只有一個父類。
(3)接口不是類分級結構的一部分,無任何聯 系的類可以實現相同的接口
二、回調(callback):
一種程序設計模式,在這種模 式中,可指出某個特定事件發生時程序應該采取 的動作。
三、Object類的Clone方法:
?當拷貝一個對象變量時,原始變量與拷貝變量 引用同一個對象。這樣,改變一個變量所引用 的對象會對另一個變量產生影響。
四、 淺層拷貝與深層拷貝:
?淺層拷貝:被拷貝對象的所有常量成員和基本類 型屬性都有與原來對象相同的拷貝值,而若成員 域是一個對象,則被拷貝對象該對象域的對象引 用仍然指向原來的對象。 ?深層拷貝:被拷貝對象的所有成員域都含有與原 來對象相同的值,且對象域將指向被復制過的新對 象,而不是原有對象被引用的對象。換言之,深 層拷貝將拷貝對象內引用的對象也拷貝一遍。
五、 Java中對象克隆的實現:
?在子類中實現Cloneable接口。 ?
? ? 為了獲取對象的一份拷貝,可以利用Object類 的clone方法。
?在子類中覆蓋超類的clone方法,聲明為 public。
?在子類的clone方法中,調用super.clone()。
? ? ?? 對象克隆要點:
–在類中實現Cloneable接口。
–為了獲取對象的一份拷貝,使用Object類的clone 方法。
–在類中覆蓋超類的clone方法,聲明為public。
–在類的clone方法中,調用super.clone()。
六、Lambda表達式:
?Java Lambda 表達式是Java 8 引入的一個新的功能,主要用途是提供一個函數化的語法來簡化編碼。
?