王之泰201771010131《面向对象程序设计(java)》第八周学习总结
第一部分:理論知識(shí)學(xué)習(xí)部分
?
第六章
第六章知識(shí)點(diǎn)主要分為1. 接口 2.?lambda表達(dá)式 3. 內(nèi)部類(lèi) 4. 代理
1. 接口
1) Java為了克服單繼承的缺點(diǎn),Java使用了接口, 一個(gè)類(lèi)可以實(shí)現(xiàn)一個(gè)或多個(gè)接口。
2) 在Java程序設(shè)計(jì)語(yǔ)言中,接口不是類(lèi),而是對(duì)類(lèi) 的一組需求描述,由常量和一組抽象方法組成。
3) 接口中不包括變量和有具體實(shí)現(xiàn)的方法。
4) 只要類(lèi)實(shí)現(xiàn)了接口,則該類(lèi)要遵從接口描述的統(tǒng) 一格式進(jìn)行定義,并且可以在任何需要該接口的 地方使用這個(gè)類(lèi)的對(duì)象。
5)聲明方式: public interface 接口名 { …… }? ?接口體中包含常量定義和方法定義,接口中只進(jìn) 行方法的聲明,不提供方法的實(shí)現(xiàn)。
6)類(lèi)似建立類(lèi)的繼承關(guān)系,接口也可以擴(kuò)展。 接口的擴(kuò)展技術(shù)使得從具有較高通用性的接口存在 多條鏈延伸到具有較高專(zhuān)用性的接口。
擴(kuò)展方法: public? ?interface? 接口1? ?extends接口2 {? ? ……? ? ? ? ? ?}
7)在類(lèi)聲明時(shí)用implements關(guān)鍵字聲明使用一個(gè)或 多個(gè)接口
class Employee implementsPrintable { …… }?
一個(gè)類(lèi)使用了某個(gè)接口,那么這個(gè)類(lèi)必須實(shí)現(xiàn)該 接口的所有方法,即為這些方法提供方法體。 一個(gè)類(lèi)可以實(shí)現(xiàn)多個(gè)接口,接口間應(yīng)該用逗號(hào)分 隔開(kāi)。
class Employee implements Cloneable,Comparable
8)接口不能構(gòu)造接口對(duì)象,但可以聲明接口變量以指向一個(gè)實(shí)現(xiàn)了該接口的類(lèi)對(duì)象。
Comparablex = new Comparable(…);? ? ? ?//ERROR
Comparable? x= new Employee(…);? ? ?//OK
可以用instanceof檢查對(duì)象是否實(shí)現(xiàn)了某個(gè)接口。
if? (anObjectinstanceofComparable) {? ?……}?
? 2.?接口與抽象類(lèi)的區(qū)別: (1)接口不能實(shí)現(xiàn)任何方法,而抽象類(lèi)可以。 (2)類(lèi)可以實(shí)現(xiàn)許多接口,但只有一個(gè)父類(lèi)。 (3)接口不是類(lèi)分級(jí)結(jié)構(gòu)的一部分,無(wú)任何聯(lián)系的類(lèi)可以實(shí)現(xiàn)相同的接口。
? ? ? ? ? ? ? ?3. 回調(diào)(callback):一種程序設(shè)計(jì)模式,在這種模式中,可指出某個(gè)特定事件發(fā)生時(shí)程序應(yīng)該采取的動(dòng)作。 在java.swing包中有一個(gè)Timer類(lèi),可以使用它在到達(dá)給定的時(shí)間間隔時(shí)觸發(fā)一個(gè)事件。
? ? ? ? ? ? ? ? ? ? ?Timer(intinterval, ActionListenerlistener)
? ? void start()
? ?void stop()
4.?Object類(lèi)的Clone方法
? ? ? 1)當(dāng)拷貝一個(gè)對(duì)象變量時(shí),原始變量與拷貝變量引用同一個(gè)對(duì)象。這樣,改變一個(gè)變量所引用 的對(duì)象會(huì)對(duì)另一個(gè)變量產(chǎn)生影響。
? ? ?2)如果要?jiǎng)?chuàng)建一個(gè)對(duì)象新的copy,它的最初狀態(tài)與 original一樣,但以后可以各自改變狀態(tài),就需要使用Object類(lèi)的clone方法。
5.?淺層拷貝:被拷貝對(duì)象的所有常量成員和基本類(lèi) 型屬性都有與原來(lái)對(duì)象相同的拷貝值,而若成員域是一個(gè)對(duì)象,則被拷貝對(duì)象該對(duì)象域的對(duì)象引 用仍然指向原來(lái)的對(duì)象。
深層拷貝:被拷貝對(duì)象的所有成員域都含有與原 來(lái)對(duì)象相同的值,且對(duì)象域?qū)⒅赶虮粡?fù)制過(guò)的新對(duì) 象,而不是原有對(duì)象被引用的對(duì)象。換言之,深 層拷貝將拷貝對(duì)象內(nèi)引用的對(duì)象也拷貝一遍。
6. Java Lambda 表達(dá)式是Java 8 引入的一個(gè)新的功能,主要用途是提供一個(gè)函數(shù)化的語(yǔ)法來(lái)簡(jiǎn)化編碼。 Lambda表達(dá)式本質(zhì)上是一個(gè)匿名方法。
public intadd(intx, inty) { return x + y; } 轉(zhuǎn)成Lambda表達(dá)式后是: (intx, inty) -> x + y;?
7.內(nèi)部類(lèi)(inner class)是定義在一個(gè)類(lèi)內(nèi)部的類(lèi)。 外層的類(lèi)成為外部類(lèi)(outer class).
? ?內(nèi)部類(lèi)主要用于事件處理。 使用內(nèi)部類(lèi)的原因有以下三個(gè):
1)內(nèi)部類(lèi)方法可以訪問(wèn)該類(lèi)定義所在的作用域中的數(shù)據(jù),包括私有數(shù)據(jù)。
2)內(nèi)部類(lèi)能夠隱藏起來(lái),不為同一包中的其他類(lèi)所見(jiàn)。
3)想要定義一個(gè)回調(diào)函數(shù)且不想編寫(xiě)大量代碼時(shí), 使用匿名內(nèi)部類(lèi)比較便捷。?
第二部分:實(shí)驗(yàn)部分?— 接口的定義與使用
實(shí)驗(yàn)時(shí)間?2018-10-18
1、實(shí)驗(yàn)?zāi)康呐c要求
(1)?掌握接口定義方法;
(2)?掌握實(shí)現(xiàn)接口類(lèi)的定義要求;
(3)?掌握實(shí)現(xiàn)了接口類(lèi)的使用要求;
(4)?掌握程序回調(diào)設(shè)計(jì)模式;
(5)?掌握Comparator接口用法;
(6)?掌握對(duì)象淺層拷貝與深層拷貝方法;
(7)?掌握Lambda表達(dá)式語(yǔ)法;
(8)?了解內(nèi)部類(lèi)的用途及語(yǔ)法要求。
2、實(shí)驗(yàn)內(nèi)容和步驟
實(shí)驗(yàn)1:?導(dǎo)入第6章示例程序,測(cè)試程序并進(jìn)行代碼注釋。
測(cè)試程序1:
1.編輯、編譯、調(diào)試運(yùn)行閱讀教材214頁(yè)-215頁(yè)程序6-1、6-2,理解程序并分析程序運(yùn)行結(jié)果;
2.在程序中相關(guān)代碼處添加新知識(shí)的注釋。
3,掌握接口的實(shí)現(xiàn)用法;
4.掌握內(nèi)置接口Compareable的用法。
EmployeeSortTest程序
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); 21 22 // 打印所有員工對(duì)象的信息 23 for (Employee e : staff) 24 System.out.println("name=" + e.getName() + ",salary=" + e.getSalary()); 25 } 26 }Employee程序
1 package interfaces; 2 3 //將類(lèi)聲明為實(shí)現(xiàn)為某個(gè)接口,使用implements關(guān)鍵字 4 public class Employee implements Comparable<Employee> 5 { 6 private String name; 7 private double salary; 8 9 public Employee(String name, double salary) 10 { 11 this.name = name; 12 this.salary = salary; 13 } 14 15 public String getName() 16 { 17 return name; 18 } 19 20 public double getSalary() 21 { 22 return salary; 23 } 24 25 public void raiseSalary(double byPercent) 26 { 27 double raise = salary * byPercent / 100; 28 salary += raise; 29 } 30 31 /** 32 * Compares employees by salary 33 * @param other another Employee object 34 * @return a negative value if this employee has a lower salary than 35 * otherObject, 0 if the salaries are the same, a positive value otherwise 36 */ 37 public int compareTo(Employee other) 38 { 39 // 這里使用了靜態(tài)Double.compare方法,且為泛型Comparable提供了一個(gè)類(lèi)型參數(shù) 40 return Double.compare(salary, other.salary); 41 } 42 }?實(shí)驗(yàn)結(jié)果:
?
測(cè)試程序2:
編輯、編譯、調(diào)試以下程序,結(jié)合程序運(yùn)行結(jié)果理解程序
1 interface A 2 { 3 double g=9.8; 4 void show( ); 5 } 6 class C implements 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); 19 } 20 }實(shí)驗(yàn)結(jié)果:
?
?
測(cè)試程序3:
1.在elipse?IDE中調(diào)試運(yùn)行教材223頁(yè)6-3,結(jié)合程序運(yùn)行結(jié)果理解程序;
2.26行、36行代碼參閱224頁(yè),詳細(xì)內(nèi)容涉及教材12章。
3.在程序中相關(guān)代碼處添加新知識(shí)的注釋。
4.掌握回調(diào)程序設(shè)計(jì)模式;
6-3代碼如下:
?
1 /** 2 @version 1.01 2015-05-12 3 @author Cay Horstmann 4 */ 5 6 import java.awt.*; 7 import java.awt.event.*; 8 import java.util.*; 9 import javax.swing.*; 10 import javax.swing.Timer; 11 // 用JavaUTIL計(jì)時(shí)器解決沖突 12 13 public class TimerTest 14 { 15 public static void main(String[] args) 16 { 17 ActionListener listener = new TimePrinter(); 18 19 // 構(gòu)建一個(gè)調(diào)用偵聽(tīng)器的計(jì)時(shí)器 20 // 每十秒一次 21 Timer t = new Timer(10000, listener); 22 t.start(); 23 24 JOptionPane.showMessageDialog(null, "Quit program?"); 25 System.exit(0); 26 } 27 } 28 29 class TimePrinter implements ActionListener 30 { 31 public void actionPerformed(ActionEvent event) 32 { 33 System.out.println("At the tone, the time is " + new Date()); 34 Toolkit.getDefaultToolkit().beep(); 35 } 36 }?
實(shí)驗(yàn)結(jié)果如下:
?
?
測(cè)試程序4:
1.調(diào)試運(yùn)行教材229頁(yè)-231頁(yè)程序6-4、6-5,結(jié)合程序運(yùn)行結(jié)果理解程序;
2.在程序中相關(guān)代碼處添加新知識(shí)的注釋。
3.掌握對(duì)象克隆實(shí)現(xiàn)技術(shù);
4.掌握淺拷貝和深拷貝的差別。
6-4程序如下:
1 package clone; 2 3 /** 4 * This program demonstrates cloning. 5 * @version 1.10 2002-07-01 6 * @author Cay Horstmann 7 */ 8 //克隆了employee中的一個(gè)實(shí)例,調(diào)用了兩個(gè)更改器的方法 9 public class CloneTest 10 { 11 public static void main(String[] args) 12 { 13 try 14 { 15 Employee original = new Employee("John Q. Public", 50000); 16 original.setHireDay(2000, 1, 1); 17 Employee copy = original.clone(); 18 copy.raiseSalary(10);//raiseSalary方法改變salary域的值 19 copy.setHireDay(2002, 12, 31);//setHireDay改變hireday域的狀態(tài) 20 System.out.println("original=" + original); 21 System.out.println("copy=" + copy); 22 } 23 catch (CloneNotSupportedException e) 24 { 25 e.printStackTrace(); 26 } 27 } 28 }?
6-5程序如下:
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) 13 { 14 this.name = name; 15 this.salary = salary; 16 hireDay = new Date(); 17 } 18 19 public Employee clone() throws CloneNotSupportedException 20 { 21 // 調(diào)用對(duì)象clone 22 Employee cloned = (Employee) super.clone(); 23 24 // 克隆可變字段 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 // 實(shí)例字段突變示例 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 }實(shí)驗(yàn)結(jié)果如下:
?
實(shí)驗(yàn)2:?導(dǎo)入第6章示例程序6-6,學(xué)習(xí)Lambda表達(dá)式用法。
1.調(diào)試運(yùn)行教材233頁(yè)-234頁(yè)程序6-6,結(jié)合程序運(yùn)行結(jié)果理解程序;
2.在程序中相關(guān)代碼處添加新知識(shí)的注釋。
將27-29行代碼與教材223頁(yè)程序?qū)Ρ?#xff0c;將27-29行代碼與此程序?qū)Ρ?#xff0c;體會(huì)Lambda表達(dá)式的優(yōu)點(diǎn)。
?6-6程序如下:
?
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)); 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) //先指定類(lèi)型,再計(jì)算表達(dá)式 25 -> first.length() - second.length()); 26 System.out.println(Arrays.toString(planets)); 27 28 Timer t = new Timer(1000, event 29 ->System.out.println("The time is " + new Date())); 30 t.start(); 31 32 // 保持程序運(yùn)行直到用戶(hù)選擇“OK” 33 JOptionPane.showMessageDialog(null, "Quit program?"); 34 System.exit(0); 35 } 36 }?
實(shí)驗(yàn)結(jié)果如下:
?
實(shí)驗(yàn)3:?編程練習(xí)
1.編制一個(gè)程序,將身份證號(hào).txt?中的信息讀入到內(nèi)存中;
2.按姓名字典序輸出人員信息;
3.查詢(xún)最大年齡的人員信息;
4.查詢(xún)最小年齡人員信息;
5.輸入你的年齡,查詢(xún)身份證號(hào).txt中年齡與你最近人的姓名、身份證號(hào)、年齡、性別和出生地;
6.查詢(xún)?nèi)藛T中是否有你的同鄉(xiāng)。
實(shí)驗(yàn)代碼如下:
?
1 package IDcard; 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.Scanner; 11 import java.util.Collections; 12 13 public class ID { 14 15 public static People findPeopleByname(String name) { 16 People flag = null; 17 for (People people : peoplelist) { 18 if(people.getName().equals(name)) { 19 flag = people; 20 } 21 } 22 return flag; 23 24 } 25 26 public static People findPeopleByid(String id) { 27 People flag = null; 28 for (People people : peoplelist) { 29 if(people.getnumber().equals(id)) { 30 flag = people; 31 } 32 } 33 return flag; 34 35 } 36 37 private static ArrayList<People> agenear(int yourage) { 38 // TODO Auto-generated method stub 39 int j=0,min=53,d_value=0,k = 0; 40 ArrayList<People> plist = new ArrayList<People>(); 41 for (int i = 0; i < peoplelist.size(); i++) { 42 d_value = peoplelist.get(i).getage() > yourage ? 43 peoplelist.get(i).getage() - yourage : yourage - peoplelist.get(i).getage() ; 44 k = d_value < min ? i : k; 45 min = d_value < min ? d_value : min; 46 } 47 for(People people : peoplelist) { 48 if(people.getage() == peoplelist.get(k).getage()) { 49 plist.add(people); 50 } 51 } 52 return plist; 53 } 54 55 private static ArrayList<People> peoplelist; 56 57 public static void main(String[] args) { 58 peoplelist = new ArrayList<People>(); 59 Scanner scanner = new Scanner(System.in); 60 File file = new File("D:\\身份證號(hào).txt"); 61 try { 62 FileInputStream files = new FileInputStream(file); 63 BufferedReader in = new BufferedReader(new InputStreamReader(files)); 64 String temp = null; 65 while ((temp = in.readLine()) != null) { 66 67 String[] information = temp.split("[ ]+"); 68 People people = new People(); 69 people.setName(information[0]); 70 people.setnumber(information[1]); 71 int A = Integer.parseInt(information[3]); 72 people.setage(A); 73 people.setsex(information[2]); 74 for(int j = 4; j<information.length;j++) { 75 people.setplace(information[j]); 76 } 77 peoplelist.add(people); 78 79 } 80 } catch (FileNotFoundException e) { 81 System.out.println("文件未找到"); 82 e.printStackTrace(); 83 } catch (IOException e) { 84 System.out.println("文件讀取錯(cuò)誤"); 85 e.printStackTrace(); 86 } 87 boolean isTrue = true; 88 while (isTrue) { 89 90 System.out.println("******************************************"); 91 System.out.println(" 1.按姓名典序輸出人員信息"); 92 System.out.println(" 2.查詢(xún)最大年齡人員信息"); 93 System.out.println(" 3.查詢(xún)最小年齡人員信息"); 94 System.out.println(" 4.輸入你的年齡,查詢(xún)身份證號(hào).txt中年齡與你最近的人"); 95 System.out.println(" 5.查詢(xún)?nèi)藛T中是否有你的同鄉(xiāng)"); 96 System.out.println(" 6.退出"); 97 System.out.println("******************************************"); 98 int nextInt = scanner.nextInt(); 99 switch (nextInt) { 100 case 1: 101 Collections.sort(peoplelist); 102 System.out.println(peoplelist.toString()); 103 break; 104 case 2: 105 int max=0; 106 int j,k1 = 0; 107 for(int i=1;i<peoplelist.size();i++) 108 { 109 j = peoplelist.get(i).getage(); 110 if(j>max) 111 { 112 max = j; 113 k1 = i; 114 } 115 116 } 117 System.out.println("年齡最大:"+peoplelist.get(k1)); 118 break; 119 case 3: 120 int min = 100; 121 int j1,k2 = 0; 122 for(int i=1;i<peoplelist.size();i++) 123 { 124 j1 = peoplelist.get(i).getage(); 125 if(j1<min) 126 { 127 min = j1; 128 k2 = i; 129 } 130 131 } 132 System.out.println("年齡最小:"+peoplelist.get(k2)); 133 break; 134 case 4: 135 System.out.println("年齡:"); 136 int input_age = scanner.nextInt(); 137 ArrayList<People> plist = new ArrayList<People>(); 138 plist = agenear(input_age); 139 for(People people : plist) { 140 System.out.println(people.toString()); 141 } 142 break; 143 case 5: 144 System.out.println("請(qǐng)輸入省份"); 145 String find = scanner.next(); 146 for (int i = 0; i <peoplelist.size(); i++) 147 { 148 String [] place = peoplelist.get(i).getplace().split("\t"); 149 for(String temp : place) { 150 if(find.equals(temp)) { 151 System.out.println("你的同鄉(xiāng)是 "+peoplelist.get(i)); 152 break; 153 } 154 } 155 156 } 157 break; 158 case 6: 159 isTrue = false; 160 System.out.println("byebye!"); 161 break; 162 default: 163 System.out.println("輸入有誤"); 164 } 165 } 166 } 167 168 } 1 package IDcard; 2 3 public class People implements Comparable<People> { 4 5 private String name = null; 6 private String number = null; 7 private int age = 0; 8 private String sex = null; 9 private String place = null; 10 11 public String getName() 12 { 13 return name; 14 } 15 public void setName(String name) 16 { 17 this.name = name; 18 } 19 public String getnumber() 20 { 21 return number; 22 } 23 public void setnumber(String number) 24 { 25 this.number = number; 26 } 27 public int getage() 28 { 29 return age; 30 } 31 public void setage(int age ) 32 { 33 this.age = age; 34 } 35 public String getsex() 36 { 37 return sex; 38 } 39 public void setsex(String sex ) 40 { 41 this.sex = sex; 42 } 43 public String getplace() 44 { 45 return place; 46 } 47 public void setplace(String place) 48 { 49 if(this.place == null) { 50 this.place = place; 51 }else { 52 this.place = this.place+ "\t" +place; 53 } 54 55 } 56 public int compareTo(People o) 57 { 58 return this.name.compareTo(o.getName()); 59 } 60 public String toString() 61 { 62 return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+place+"\n"; 63 } 64 }?
實(shí)驗(yàn)結(jié)果如下:
?
?
實(shí)驗(yàn)4:內(nèi)部類(lèi)語(yǔ)法驗(yàn)證實(shí)驗(yàn)
實(shí)驗(yàn)程序1:
1.編輯、調(diào)試運(yùn)行教材246頁(yè)-247頁(yè)程序6-7,結(jié)合程序運(yùn)行結(jié)果理解程序;
2.了解內(nèi)部類(lèi)的基本用法。
6-7程序如下:
?
1 package innerClass; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.*; 6 import javax.swing.*; 7 import javax.swing.Timer; 8 9 /** 10 * This program demonstrates the use of inner classes. 11 * @version 1.11 2015-05-12 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); 19 clock.start(); 20 21 // keep program running until 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 ActionListener listener = new TimePrinter(); 52 Timer t = new Timer(interval, listener); 53 t.start(); 54 } 55 56 public class TimePrinter implements ActionListener 57 { 58 public void actionPerformed(ActionEvent event) 59 { 60 System.out.println("At the tone, the time is " + new Date()); 61 if (beep) Toolkit.getDefaultToolkit().beep(); 62 } 63 } 64 }?
實(shí)驗(yàn)結(jié)果如下:
?
實(shí)驗(yàn)程序2:
1.編輯、調(diào)試運(yùn)行教材254頁(yè)程序6-8,結(jié)合程序運(yùn)行結(jié)果理解程序;
2.了解匿名內(nèi)部類(lèi)的用法。
6-8程序如下:
?
1 package anonymousInnerClass; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.*; 6 import javax.swing.*; 7 import javax.swing.Timer; 8 9 /** 10 * This program demonstrates anonymous inner classes. 11 * @version 1.11 2015-05-12 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 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() 40 { 41 public void actionPerformed(ActionEvent event) 42 { 43 System.out.println("At the tone, the time is " + new Date()); 44 if (beep) Toolkit.getDefaultToolkit().beep(); 45 } 46 }; 47 Timer t = new Timer(interval, listener); 48 t.start(); 49 } 50 }?
實(shí)驗(yàn)結(jié)果如下:
?
?
實(shí)驗(yàn)程序3:
1.在elipse?IDE中調(diào)試運(yùn)行教材257頁(yè)-258頁(yè)程序6-9,結(jié)合程序運(yùn)行結(jié)果理解程序;
2.了解靜態(tài)內(nèi)部類(lèi)的用法。
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[] d = new double[20]; 13 for (int i = 0; i < d.length; i++) 14 d[i] = 100 * Math.random(); 15 ArrayAlg.Pair p = ArrayAlg.minmax(d); 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 }?
實(shí)驗(yàn)結(jié)果如下:
?第三部分:總結(jié)
? 通過(guò)本周的學(xué)習(xí)我收益很多,比如,知道了接口和繼承之間的區(qū)別,object類(lèi)的clone方法等,并在助教學(xué)長(zhǎng)的指導(dǎo)下改善了上周測(cè)試題中的不足。本周的實(shí)驗(yàn)主要還是以理解課本中的代碼案例為主,老師為了節(jié)省我們的時(shí)間,自主完成實(shí)驗(yàn)也只是在以前實(shí)驗(yàn)的基礎(chǔ)上添加了新的知識(shí)內(nèi)容,在課后探討學(xué)習(xí)中助教學(xué)長(zhǎng)給我們?cè)敿?xì)講解了不理解的地方,拓展了我們的視野,展示了Java做工程的強(qiáng)大能力。更加加深了我們對(duì)Java的學(xué)習(xí)興趣。
轉(zhuǎn)載于:https://www.cnblogs.com/hackerZT-7/p/9811837.html
總結(jié)
以上是生活随笔為你收集整理的王之泰201771010131《面向对象程序设计(java)》第八周学习总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 网格计算和云计算之间的比较
- 下一篇: 【学习笔记】monitor