吳麗麗-201871010123《面向對象程序設計(Java)》第七週學習總結html
項目 | 內容 |
這個做業屬於哪一個課程 | http://www.cnblogs.com/nwnu-daizh/java |
這個做業的要求在哪裏 | https://www.cnblogs.com/nwnu-daizh/p/11654436.html |
做業的學習目標 | (1) 掌握四種訪問權限修飾符的使用特色;編程 (2) 掌握Object類的用途及經常使用API;數組 (3) 掌握ArrayList類的定義方法及用法;ide (4)掌握枚舉類定義方法及用途;函數 (5)結合本章實驗內容,理解繼承與多態性兩個面向對象程序設計特徵,並體會其優勢。學習 |
實驗內容和步驟測試
實驗1:在「System.out.println(...);」語句處按註釋要求設計代碼替換...,觀察代碼錄入中IDE提示,以驗證四種權限修飾符的用法。this
package project; 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(p2);//分別嘗試顯示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.p2); System.out.println(parent.p3); System.out.println(parent.p4);//分別嘗試用parent調用Paren類的方法、用son調用Son類的方法
System.out.println(son.p2);
System.out.println(son.p3);
System.out.println(son.p4);
System.out.println(son.s2);
System.out.println(son.s3);
System.out.println(son.s4);
parent.pMethod2();
son.pMethod2();
son.sMethod1(); } }
該代碼是將子類父類以及主函數放在同一個包的同一個文件中,其除了parent類的private不可調用外,其餘三個類均可以。子類除了不能夠調用父類的私有域外,一樣能夠調用父類的其餘域和方法。spa
程序結果運行以下:
如下是將parent類從新放入一個新的文件中:
parent類代碼以下:
package project; 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無修飾符修飾的方法"); } }
son類代碼以下:
package project; 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無修飾符修飾的方法"); } } public class Demo { public static void main(String[] args) { Parent parent=new Parent(); Son son=new Son(); System.out.println(parent.p2); System.out.println(parent.p3); System.out.println(parent.p4); System.out.println(son.s2); System.out.println(son.s3); parent.pMethod3(); son.sMethod1(); son.pMethod3(); } }
程序運行結果以下:
下面是將parent類放入以本身名字命名的包裏面
如下是Demo.java文件裏的相關代碼:
package project; import wll.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(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無修飾符修飾的方法"); } } public class Demo { public static void main(String[] args) { Parent parent=new Parent(); Son son=new Son(); System.out.println(parent.p2); System.out.println(son.s2); System.out.println(son.s3); son.sMethod1(); } }
如下是parent類的代碼:
package wll; 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無修飾符修飾的方法"); } }
如下是放入不一樣包裏的運行結果:
這個實驗主要是讓咱們瞭解關於四種訪問權限的修飾符:
位置 | private | 默認 | protected | public |
同一個類 | 是 | 是 | 是 | 是 |
同一個包裏的類 | 否 | 是 | 是 | 是 |
不一樣包內的子類 |
否 | 否 | 是 | 是 |
不一樣包而且不是子類 | 否 | 否 | 否 | 是 |
實驗2:導入第5章如下示例程序,測試並進行代碼註釋。
測試程序1:
a)運行教材程序5-八、5-九、5-10,結合程序運行結果理解程序(教材174頁-177頁);
b) 刪除程序中Employee類、Manager類中的equals()、hasCode()、toString()方法,背錄刪除方法,在代碼錄入中理解類中重寫Object父類方法的技術要點。
Employee類代碼以下:
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語句判斷兩個引用是不是同一個,若是是那麼這兩個對象確定相等 if (this == otherObject) return true; // 若是顯式參數爲空,則必須返回false if (otherObject == null) return false; // getClass()方法是獲得對象的類,若是兩個對象的類不同,那麼就不相等 if (getClass() != otherObject.getClass()) return false; //如今咱們知道另外一個對象是非空僱員 //在以上判斷完成,再將獲得的參數對象強制轉換爲該對象,考慮到父類引用子類的對象的出現,而後再判斷對象的屬性是否相同 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); } // toString()方法,可自動生成 public String toString() { return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]"; } }
EqualsTest類代碼以下:
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()); } }
Manager類代碼以下:
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; } public boolean equals(Object otherObject) { if (!super.equals(otherObject)) return false; var other = (Manager) otherObject; //檢查這個和其餘屬於同一個類 return bonus == other.bonus; } public int hashCode() { return java.util.Objects.hash(super.hashCode(), bonus); } public String toString() { return super.toString() + "[bonus=" + bonus + "]"; } }
其運行結果以下:
刪除程序中Employee類、Manager類中的equals()、hasCode()、toString()方法,背錄刪除方法後代碼以下:
Override後Manager類代碼以下:
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); // TODO Auto-generated constructor stub bonus = 0; } public void setBonus(double bonus) { this.bonus = bonus; } @Override public double getSalary() { // TODO Auto-generated method stub double baseSalary= super.getSalary(); return baseSalary+bonus; } @Override public boolean equals(Object otherObject) { // TODO Auto-generated method stub if(!super.equals(otherObject)) return false; Manager other=(Manager)otherObject; return bonus==other.bonus; } @Override public int hashCode() { // TODO Auto-generated method stub return super.hashCode()+17*new Double(bonus).hashCode(); } @Override public String toString() { // TODO Auto-generated method stub return super.toString()+"[bonus="+bonus+"]"; } }
Override後Employee代碼以下:
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; } @Override public boolean equals(Object otherObject) { // TODO Auto-generated method stub if(this==otherObject) return true; if(this==null) return false; if(getClass() != otherObject.getClass()) return false; Employee other=(Employee)otherObject; return Objects.equals(name,other.name)&&salary == other.salary&&Objects.equals(hireDay,other.hireDay); } @Override public int hashCode() { // TODO Auto-generated method stub return Objects.hash(name,salary,hireDay); } @Override public String toString() { // TODO Auto-generated method stub return getClass().getName()+"[name="+name+",salary="+salary+",hireday="+hireDay+"]"; } }
其運行結果以下:
測試程序2:
a) 在elipse IDE中調試運行程序5-11(教材182頁),結合程序運行結果理解程序;
b) 掌握ArrayList類的定義及用法;
c) 在程序中相關代碼處添加新知識的註釋;
e)設計適當的代碼,測試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) { // fill the staff array list with three Employee objects ArrayList<Employee> staff = new ArrayList<Employee>(); //聲明和構造一個保存Employee對象的數組列表 staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));//使用add方法將元素添加到數組列表中 staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1)); staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15)); // raise everyone's salary by 5% for (Employee e : staff) e.raiseSalary(5); // print out information about all Employee objects for (Employee e : staff) System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); } }
Employee類代碼以下:
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; } }
運行結果以下:
在程序中添加remove(),get(),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) { // fill the staff array list with three Employee objects ArrayList<Employee> staff = new ArrayList<Employee>(); //聲明和構造一個保存Employee對象的數組列表 staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));//使用add方法將元素添加到數組列表中 staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1)); staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15)); staff.remove(1); //從數組列表中刪除元素 int n = staff.size(); System.out.println(n); System.out.println(staff.get(0)!=staff.get(1)); // raise everyone's salary by 5% for (Employee e : staff) e.raiseSalary(5); // print out information about all Employee objects for (Employee e : staff) System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); } }
測試程序3:
a) 編輯、編譯、調試運行程序5-12(教材189頁),結合運行結果理解程序;
b)掌握枚舉類的定義及用法;
c)在程序中相關代碼處添加新知識的註釋;
d)刪除程序中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); //靜態方法valueOf 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; }
程序運行結果以下:
刪除程序中Size枚舉類override後代碼以下:
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); //靜態方法valueOf 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; } @Override public String toString() { // TODO Auto-generated method stub return super.toString(); } public String getAbbreviation() { return abbreviation; } public void setAbbreviation(String abbreviation) { this.abbreviation = 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(); dealArray(1, 2, 3); } }
程序運行結果以下:
實驗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()
補全代碼後以下:
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 { Son(){ super(false); System.out.println("Son's Constructor without parameter");
public void method(){ System.out.println("Son's method()");
super.method(); } }
程序運行結果以下:
第二部分:實驗總結
在本次實驗中我掌握了四種訪問權限修飾符的用法及其特色,Object類的用途及經常使用的API,而且瞭解了有關ArrayList類的定義及用法,知道了枚舉類定義的方法,可是對枚舉類的方法和用途還不太清楚,還有點模糊,對ArrayList的方法還不夠熟練,在本次實驗3中補全定義,感受本身作得不太好,對繼承之間的關係有點混亂,以後本身會經過問老師或者助教來將相關知識點弄懂掌握,同時也會經過線上的學習將知識點再次鞏固。