項目html |
內容java |
這個做業屬於哪一個課程編程 |
https://www.cnblogs.com/nwnu-daizh/數組 |
這個做業的要求在哪裏ide |
https://www.cnblogs.com/nwnu-daizh/p/11654436.html學習 |
做業學習目標測試 |
(1) 掌握四種訪問權限修飾符的使用特色;ui (2) 掌握Object類的用途及經常使用API;this (3) 掌握ArrayList類的定義方法及用法;spa (4)掌握枚舉類定義方法及用途; (5)結合本章實驗內容,理解繼承與多態性兩個面向對象程序設計特徵,並體會其優勢。 |
實驗內容和實驗步驟:
實驗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類的方法 } }
這個程序主要用來分析和幫助咱們理解四個修飾符的做用與區別,最重要的是咱們要理解在繼承關係中,那些修飾符修飾的父類方法子類能夠繼承。實驗輸出結果以下:
特別的,咱們要注意由private修飾的父類方法子類不能繼承。
父類(parent)程序代碼爲:
public class parent { 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();//分別嘗試顯示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類的方法 System.out.println(parent.p2); System.out.println(son.p4); System.out.println(son.s3); parent.pMethod2(); parent.pMethod4(); son.pMethod3(); } }
當(parent類)在一名字命名的新建包下面時的代碼爲:
package wangfang; public class parent { 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無修飾符修飾的方法"); } } }
輸出結果截圖爲:
主要研究子類與父類見的繼承關係以及特色:以下圖所示
此圖爲四個修飾符的訪問特色。
實驗2:導入第5章如下示例程序,測試並進行代碼註釋。
測試程序1:
l 運行教材程序5-8、5-9、5-10,結合程序運行結果理解程序(教材174頁-177頁);
l 刪除程序中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) { // a quick test to see if the objects are identical if (this == otherObject) return true; // must return false if the explicit parameter is null if (otherObject == null) return false; // if the classes don't match, they can't be equal if (getClass() != otherObject.getClass()) return false; // now we know otherObject is a non-null Employee var other = (Employee) otherObject; // test whether the fields have identical values 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 + "]"; } }
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; // 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 + "]"; } }
程序輸出結果截圖爲:
刪除程序中Employee類、Manager類中的equals()、hasCode()、toString()方法背錄刪除方法,在代碼錄入中理解類中重寫Object父類方法後的代碼爲:
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:
l 在elipse IDE中調試運行程序5-11(教材182頁),結合程序運行結果理解程序;
l 掌握ArrayList類的定義及用法;
l 在程序中相關代碼處添加新知識的註釋;
l 設計適當的代碼,測試ArrayList類的set()、get()、remove()、size()等方法的用法。
ArrayListTest的程序代碼爲:
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 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)); // 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; } }
程序輸出結果截圖爲:
在程序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)); 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:
l 編輯、編譯、調試運行程序5-12(教材189頁),結合運行結果理解程序;
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; }
程序輸出結果截圖爲:
刪除程序中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:錄入如下代碼,結合程序運行結果瞭解方法的可變參數用法
程序代碼爲:
package test; 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); } }
程序輸出結果截圖爲:
實驗: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"); System.out.println("Son's method()"); } }
實驗輸出結果如圖所示:
實驗總結:
(一)在本週咱們主要複習了繼承的知識點,在複習過程當中尤爲注重學習了四個修飾符(private,toString,protected,默認)的特色。經過實驗更加直觀的讓咱們瞭解子類與父類在繼承過程當中哪些方法可繼承而那些不可繼承。
(二)另外,咱們再次經過實驗學習了Object類,ArrayList類以及枚舉類的特色,另外在實驗測試過程當中,我發現本身動手操做過程的能力不行,之後我會盡可能多實驗,多動手操做。並且在實驗過程當中的每一小步都應該仔細,謹慎,避免小錯誤。