實驗七 繼承附加實驗
實驗七繼承附加實驗
實驗時間 2018-10-11
1、實驗目的與要求
(1)進一步理解4個成員訪問權限修飾符的用途;
(2)掌握Object類的常用API用法;
(3)掌握ArrayList類用法與常用API;
(4)掌握枚舉類使用方法;
(5)結合本章知識,理解繼承與多態性兩個面向對象程序設計特征,並體會其優點;
(6)熟練掌握Java語言中基於類、繼承技術構造程序的語法知識(ch1-ch5);
(7)利用已掌握Java語言程序設計知識,學習設計開發含有1個主類、2個以上用戶自定義類的應用程序。
2、實驗內容和步驟
實驗1 補充以下程序中主類內main方法體,以驗證四種權限修飾符的用法。
public class TEST1 { private String t1 = "這是TEST1的私有屬性"; public String t2 = "這是TEST1的公有屬性"; protected String t3 = "這是TEST1受保護的屬性"; String t4 = "這是TEST1的默認屬性"; private void tese1() { System.out.println("我是TEST1用private修飾符修飾的方法"); } public void tese2() { System.out.println("我是TEST1用public修飾符修飾的方法"); } protected void tese3() { System.out.println("我是TEST1用protected修飾符修飾的方法"); } void tese4() { System.out.println("我是TEST1無修飾符修飾的方法"); } } public class TEST2 extends TEST1{ private String e1 = "這是TEST2的私有屬性"; public String e2 = "這是TEST2的公有屬性"; protected String e3 = "這是TEST2受保護的屬性"; String e4 = "這是TEST2的默認屬性"; public void demo1() { System.out.println("我是TEST2用public修飾符修飾的方法"); } private void demo2() { System.out.println("我是TEST2用private修飾符修飾的方法"); } protected void demo3() { System.out.println("我是TEST2用protected修飾符修飾的方法"); } void demo4() { System.out.println("我是TEST2無修飾符修飾的方法"); } } public class Main { public static void main(String[] args) { TEST2 test2 = new TEST2(); /*以下設計代碼分別調用 demo1 demo2 demo3 demo4 test1 test2 test3 test4方法和t1 t2 t3 t3 e1 e2 e3 e4屬性,結合程序運行結果理解繼承和權限修飾符的用法與區別*/ } } |
實驗2 第五章測試程序反思,繼承知識總結。
測試程序1:
? 編輯、編譯、調試運行教材程序5-8、5-9、5-10(教材174頁-177頁);
? 結合程序運行結果,理解程序代碼,掌握Object類的定義及用法;
package equals; import java.time.*; import java.util.Objects; public class Employee { private String name; private double salary; private LocalDate hireDay; //屬性 public Employee(String name, double salary, int year, int month, int day) { this.name = name; this.salary = salary; hireDay = LocalDate.of(year, month, day); } //構造器 public String getName() { return name; } //訪問器 public double getSalary() { return salary; } //訪問器
public LocalDate getHireDay() { return hireDay; } //訪問器 public void raiseSalary(double byPercent) { double raise = salary * byPercent / 100; salary += raise; } //完成漲工資的計算 public boolean equals(Object otherObject) { //快速測試,看看這些對象是否相同
if (this == otherObject) return true; // 如果顯式參數為空,則必須返回false
if (otherObject == null) return false; // 如果類不匹配,它們就不能相等 if (getClass() != otherObject.getClass()) return false; // 現在我們知道otherObject是一個非空雇員
Employee other = (Employee) otherObject; // 測試字段是否具有相同的值
return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay); } public int hashCode() //hashcode返回散列碼 { return Objects.hash(name, salary, hireDay); } public String toString() //返回類對象的狀態信息 { return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]"; } }
package equals; /** * This program demonstrates the equals method. * @version 1.12 2012-01-26 * @author Cay Horstmann */ public class EqualsTest { public static void main(String[] args) { Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15); Employee alice2 = alice1; Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15); Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1); System.out.println("alice1 == alice2: " + (alice1 == alice2)); System.out.println("alice1 == alice3: " + (alice1 == alice3)); System.out.println("alice1.equals(alice3): " + alice1.equals(alice3)); System.out.println("alice1.equals(bob): " + alice1.equals(bob)); System.out.println("bob.toString(): " + bob); Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15); Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15); boss.setBonus(5000); System.out.println("boss.toString(): " + boss); System.out.println("carl.equals(boss): " + carl.equals(boss)); System.out.println("alice1.hashCode(): " + alice1.hashCode()); System.out.println("alice3.hashCode(): " + alice3.hashCode()); System.out.println("bob.hashCode(): " + bob.hashCode()); System.out.println("carl.hashCode(): " + carl.hashCode()); } }
1 package equals; 2 3 public class Manager extends Employee 4 { 5 private double bonus; 6 7 public Manager(String name, double salary, int year, int month, int day) 8 { 9 super(name, salary, year, month, day); 10 bonus = 0; 11 } 12 13 public double getSalary() 14 { 15 double baseSalary = super.getSalary(); 16 return baseSalary + bonus; 17 } 18 19 public void setBonus(double bonus) 20 { 21 this.bonus = bonus; 22 } 23 24 public boolean equals(Object otherObject) 25 { 26 if (!super.equals(otherObject)) return false; 27 Manager other = (Manager) otherObject; 28 //super.equals檢查這個和其他屬於同一個類
29 return bonus == other.bonus; 30 } 31 32 public int hashCode() 33 { 34 return java.util.Objects.hash(super.hashCode(), bonus); 35 } 36 37 public String toString() 38 { 39 return super.toString() + "[bonus=" + bonus + "]"; 40 } 41 }
測試程序2:
? 編輯、編譯、調試運行教材程序5-11(教材182頁);
? 結合程序運行結果,理解程序代碼,掌握ArrayList類的定義及用法;
代碼如下
1 package arrayList; 2 3 import java.util.*; 4 5 /** 6 * This program demonstrates the ArrayList class. 7 * @version 1.11 2012-01-26 8 * @author Cay Horstmann 9 */ 10 public class ArrayListTest 11 { 12 public static void main(String[] args) 13 { 14 // 用三個Employee對象填充staff數組列表
15 ArrayList<Employee> staff = new ArrayList<>(); 16 17 staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15)); 18 staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1)); 19 staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15)); 20 21 // 把每個人的工資提升百分之五
22 for (Employee e : staff) 23 e.raiseSalary(5); 24 25 // 輸出所有雇員對象的信息
26 for (Employee e : staff) 27 System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 28 + e.getHireDay()); 29 } 30 }
1 package arrayList; 2 3 import java.time.*; 4 5 public class Employee 6 { 7 private String name; 8 private double salary; 9 private LocalDate hireDay; 10 11 public Employee(String name, double salary, int year, int month, int day) 12 { 13 this.name = name; 14 this.salary = salary; 15 hireDay = LocalDate.of(year, month, day); 16 } 17 18 public String getName() 19 { 20 return name; 21 } 22 23 public double getSalary() 24 { 25 return salary; 26 } 27 28 public LocalDate getHireDay() 29 { 30 return hireDay; 31 } 32 33 public void raiseSalary(double byPercent) 34 { 35 double raise = salary * byPercent / 100; 36 salary += raise; 37 } 38 }
測試程序3:
? 編輯、編譯、調試運行程序5-12(教材189頁);
? 結合運行結果,理解程序代碼,掌握枚舉類的定義及用法;
代碼如下
1 package enums; 2 3 import java.util.*; 4 5 /** 6 * This program demonstrates enumerated types. 7 * @version 1.0 2004-05-24 8 * @author Cay Horstmann 9 */ 10 public class EnumTest 11 { 12 public static void main(String[] args) 13 { 14 Scanner in = new Scanner(System.in); 15 System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) "); 16 String input = in.next().toUpperCase(); 17 Size size = Enum.valueOf(Size.class, input); 18 System.out.println("size=" + size); 19 System.out.println("abbreviation=" + size.getAbbreviation()); 20 if (size == Size.EXTRA_LARGE) 21 System.out.println("Good job--you paid attention to the _."); 22 } 23 } 24 25 enum Size 26 { 27 SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL"); 28 29 private Size(String abbreviation) { this.abbreviation = abbreviation; } 30 public String getAbbreviation() { return abbreviation; } 31 32 private String abbreviation; 33 }
實驗3:采用個人賬號登錄https://pintia.cn/,完成《2018秋季西北師範大學面向對象程序設計(Java)(ch1-ch5)測試題2》,測試時間60分鐘;
實驗4: 課後完成實驗3未完成的測試內容。
實驗總結:這次附加實驗在上次實驗的基礎上,對第五章的內容有了更加深入的了解,尤其是對於四種權限修飾符的使用。此外掌握Object類的常用API用法,掌握ArrayList類用法與和枚舉類使用方法;
有所提高,仍需努力。
實驗七 繼承附加實驗