201871010111-劉佳華《面向對象程序設計(java)》第七週學習總結java
實驗時間 2019-10-11編程
1、實驗目的與要求數組
1) 掌握四種訪問權限修飾符的使用特色;ide
(1)進一步理解4個成員訪問權限修飾符的用途;學習
A.僅對本類可見-private測試
B.對全部類可見-publicthis
C.對本包和全部子類可見-protectedspa
D.對本包可見-默認,,不須要修飾符設計
2) 掌握Object類的用途及經常使用API;3d
3) 掌握ArrayList類的定義方法及用法;
4)掌握枚舉類定義方法及用途;
5)結合本章實驗內容,理解繼承與多態性兩個面向對象程序設計特徵,並體會其優勢。
2、實驗內容和步驟
實驗1: 在「System.out.println(...);」語句處按註釋要求設計代碼替換...,觀察代碼錄入中IDE提示,以驗證四種權限修飾符的用法。
class Parent { private String p1 = "這是Parent的私有屬性"; public String p2 = "這是Parent的公有屬性"; protected String p3 = "這是Parent受保護的屬性"; String p4 = "這是Parent的默認屬性"; private void pMethod1() { System.out.println("我是Parent用private修飾符修飾的方法"); } public void pMethod2() { System.out.println("我是Parent用public修飾符修飾的方法"); } protected void pMethod3() { System.out.println("我是Parent用protected修飾符修飾的方法"); } void pMethod4() { System.out.println("我是Parent無修飾符修飾的方法"); } } class Son extends Parent{ private String s1 = "這是Son的私有屬性"; public String s2 = "這是Son的公有屬性"; protected String s3 = "這是Son受保護的屬性"; String s4 = "這是Son的默認屬性"; public void sMethod1() { System.out.println(...);//分別嘗試顯示Parent類的p一、p二、p三、p4值 System.out.println("我是Son用public修飾符修飾的方法"); } private void sMethod2() { System.out.println("我是Son用private修飾符修飾的方法"); } protected void sMethod() { System.out.println("我是Son用protected修飾符修飾的方法"); } void sMethod4() { System.out.println("我是Son無修飾符修飾的方法"); } } public class Demo { public static void main(String[] args) { Parent parent=new Parent(); Son son=new Son(); System.out.println(...); //分別嘗試用parent調用Paren類的方法、用son調用Son類的方法 } } |
public class Demo { public static void main(String[] args) { Parent parent=new Parent(); Son son=new Son(); //System.out.println(...); //分別嘗試用parent調用Parent類的方法、訪問值; parent.pMethod2(); parent.pMethod3(); parent.pMethod4();//因爲pMethod1爲私有的,因此不能用parent.pMethod1調用 System.out.println(parent.p2); System.out.println(parent.p3); System.out.println(parent.p4); System.out.println("**********************************"); //用son調用Son類的方法 son.sMethod1(); son.sMethod3(); son.sMethod4(); System.out.println("**********************************"); //用son調用parent類的方法 son.pMethod2(); System.out.println("**********************************"); } }
package SY1; public class Parent { private String p1 = "這是Parent的私有屬性"; public String p2 = "這是Parent的公有屬性"; protected String p3 = "這是Parent受保護的屬性"; String p4 = "這是Parent的默認屬性"; private void pMethod1() { System.out.println("我是Parent用private修飾符修飾的方法"); } public void pMethod2() { System.out.println("我是Parent用public修飾符修飾的方法"); } protected void pMethod3() { System.out.println("我是Parent用protected修飾符修飾的方法"); } void pMethod4() { System.out.println("我是Parent無修飾符修飾的方法"); } }
package SY1; Public class Son extends Parent{ private String s1 = "這是Son的私有屬性"; public String s2 = "這是Son的公有屬性"; protected String s3 = "這是Son受保護的屬性"; String s4 = "這是Son的默認屬性"; public void sMethod1() { System.out.println(p2);//分別嘗試顯示Parent類的p一、p二、p三、p4值 System.out.println("我是Son用public修飾符修飾的方法"); } private void sMethod2() { System.out.println("我是Son用private修飾符修飾的方法"); } protected void sMethod3() { System.out.println("我是Son用protected修飾符修飾的方法"); } void sMethod4() { System.out.println("我是Son無修飾符修飾的方法"); } }
運行截圖:
@@當新建一個以LJH的包,把Parent.java文件移動到LJH的包中時,經過在Damo.java 中加入import LJH.Parent;
可是因爲Damo.java和Parent.java 不在同一個包中,一些屬性及方法將在訪問時產生錯誤,這也讓我深入認識到了權限修飾符的做用;以下圖:
實驗2:導入第5章如下示例程序,測試並進行代碼註釋。
測試程序1:
l 運行教材程序5-八、5-九、5-10,結合程序運行結果理解程序(教材174頁-177頁);
l 刪除程序中Employee類、Manager類中的equals()、hasCode()、toString()方法,背錄刪除方法,在代碼錄入中理解類中重寫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是一個非空僱員 var other = (Employee) otherObject; // 測試字段是否具備相同的值 return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay); } public int 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) { var alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15); var alice2 = alice1; var alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15); var 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); var carl = new Manager("Carl Cracker", 80000, 1987, 12, 15); var 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()); } }
package equals; public class Manager extends Employee { private double bonus; public Manager(String name, double salary, int year, int month, int day) { super(name, salary, year, month, day); bonus = 0; } public double getSalary() { double baseSalary = super.getSalary(); return baseSalary + bonus; } public void setBonus(double bonus) { this.bonus = bonus; } @Override public boolean equals(Object otherObject) { // TODO Auto-generated method stub if (!super.equals(otherObject)) return false; var other = (Manager) otherObject; // super.equals檢查這個和其餘屬於同一個類 return bonus == other.bonus; } @Override public int hashCode() { // TODO Auto-generated method stub return super.hashCode(); } @Override public String toString() { // TODO Auto-generated method stub return super.toString(); } /*public boolean equals(Object otherObject) { if (!super.equals(otherObject)) return false; var other = (Manager) otherObject; // super.equals checked that this and other belong to the same class return bonus == other.bonus; } public int hashCode() { return java.util.Objects.hash(super.hashCode(), bonus); } public String toString() { return super.toString() + "[bonus=" + bonus + "]"; }*/ }
運行結果:
測試程序2:
l 在elipse IDE中調試運行程序5-11(教材182頁),結合程序運行結果理解程序;
l 掌握ArrayList類的定義及用法;
l 在程序中相關代碼處添加新知識的註釋;
l 設計適當的代碼,測試ArrayList類的set()、get()、remove()、size()等方法的用法。
package arrayList; import java.util.*; /** * This program demonstrates the ArrayList class. * @version 1.11 2012-01-26 * @author Cay Horstmann */ public class ArrayListTest { public static void main(String[] args) { // 用三個Employee對象填充staff數組列表 var staff = new ArrayList<Employee>(); staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15)); staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1)); staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15)); // 把每一個人的薪水提升5% for (Employee e : staff) e.raiseSalary(5); // 打印全部Employee對象的信息 for (Employee e : staff) System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); } }
package arrayList; import java.time.*; 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; } }
運行結果:
更改後的ArrayListTest代碼以下:
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 var staff = new ArrayList<Employee>(); 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 System.out.println(staff.get(1)); 22 staff.set(1, new Employee("liujiahua", 50000, 2019, 10, 11)); 23 System.out.println(staff.size()); 24 staff.remove(2); 25 26 // 把每一個人的薪水提升5% 27 for (Employee e : staff) 28 e.raiseSalary(5); 29 30 // 打印全部Employee對象的信息 31 for (Employee e : staff) 32 System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 33 + e.getHireDay()); 34 } 35 }
運行代碼以後截圖:
能夠看到,在修改代碼以後運行能夠看出,經過get()方法得到下標爲1的元素儲存位置,即地址經過;set()方法,修改在ArrayList<>中下標1添加new Employee("liujiahua", 50000, 2019, 10, 11);
經過get()方法得到數組下標爲1的元素輸出、經過remove()方法刪除下標爲2的數組元素、而且經過size()方法代替.length獲得數組的長度。
測試程序3:
l 編輯、編譯、調試運行程序5-12(教材189頁),結合運行結果理解程序;
l 掌握枚舉類的定義及用法;
l 在程序中相關代碼處添加新知識的註釋;
l 刪除程序中Size枚舉類,背錄刪除代碼,在代碼錄入中掌握枚舉類的定義要求。
package enums; import java.util.*; /** * This program demonstrates enumerated types. * @version 1.0 2004-05-24 * @author Cay Horstmann */ public class EnumTest { public static void main(String[] args) { var in = new Scanner(System.in); System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) "); String input = in.next().toUpperCase(); Size size = Enum.valueOf(Size.class, input); System.out.println("size=" + size); System.out.println("abbreviation=" + size.getAbbreviation()); if (size == Size.EXTRA_LARGE) System.out.println("Good job--you paid attention to the _."); } } enum Size { SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL"); private Size(String abbreviation) { this.abbreviation = abbreviation; } public String getAbbreviation() { return abbreviation; } private String abbreviation; }
運行結果:
測試程序4:錄入如下代碼,結合程序運行結果瞭解方法的可變參數用法
public class TestVarArgus { public static void dealArray(int... intArray){ for (int i : intArray) System.out.print(i +" ");
System.out.println(); } public static void main(String args[]){ dealArray(); dealArray(1); dealArray(1, 2, 3); dealArray(1, 2, 3, 4); dealArray(1, 2, 3, 4, 5); dealArray(1, 2, 3, 4, 5, 6); } } |
運行截圖:
實驗:3:編程練習:參照輸出樣例補全程序,使程序輸出結果與輸出樣例一致。
public class Demo { public static void main(String[] args) { Son son = new Son(); son.method(); } } class Parent { Parent() { System.out.println("Parent's Constructor without parameter"); } Parent(boolean b) { System.out.println("Parent's Constructor with a boolean parameter"); } public void method() { System.out.println("Parent's method()"); } } class Son extends Parent { //補全本類定義 } |
程序運行結果以下:
Parent's Constructor with a boolean parameter
Son's Constructor without parameter
Son's method()
Parent's method()
代碼以下:
package LJH; public class Demo { public static void main(String[] args) { Son son = new Son(); son.method(); } } class Parent { Parent() { System.out.println("Parent's Constructor without parameter"); } Parent(boolean b) { System.out.println("Parent's Constructor with a boolean parameter"); } public void method() { System.out.println("Parent's method()"); } } class Son extends Parent { void Parent() { System.out.println("Son's Constructor without parameter"); } public void method() { Parent(); System.out.println("Son's method()"); super.method(); } //補全本類定義 } /* Parent's Constructor with a boolean parameter Son's Constructor without parameter Son's method() Parent's method()*/
運行結果:
實驗總結:
經過本週的實驗,我掌握理解了成員訪問權限的四個修飾符,Object類和ArrayList類的經常使用方法,API以及枚舉使用方法。在處理一些問題中,我瞭解到了個人不足,編程能力還遠遠不行。在繼承學習中,仍有一些父類和子類的關係沒搞懂;繼承程序構造技術還不太熟練,須要繼續學習鞏固。