項目html |
內容java |
這個做業屬於哪一個課程編程 |
|
這個做業的要求在哪裏ide |
https://www.cnblogs.com/nwnu-daizh/p/11654436.html函數 |
做業學習目標學習 |
|
實驗內容和步驟測試
實驗1: 在「System.out.println(...);」語句處按註釋要求設計代碼替換...,觀察代碼錄入中IDE提示,以驗證四種權限修飾符的用法。ui
package Demo; 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類的方法 } }
運行結果:this
父類與子類在同一個包內,子類能夠直接訪問父類public、proteced與默認訪問特性的成員,不能直接訪問private成員:
子類與父類不在同一個包內,子類繼承父類public成員變量做爲子類的成員變量以及方法:
實驗2:導入第5章如下示例程序,測試並進行代碼註釋。
測試程序1:
運行教材程序5-8、5-9、5-10,結合程序運行結果理解程序(教材174頁-177頁);
刪除程序中Employee類、Manager類中的equals()、hasCode()、toString()方法,背錄刪除方法,在代碼錄入中理解類中重寫Object父類方法的技術要點。
5-8源代碼:
package equals; /** * This program demonstrates the equals method. * @version 1.12 2012-01-26 * @author Cay Horstmann */ public class EqualsTest //此程序實現了Employee類和Manager類的equals,hashCode,toString方法 { 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()); } }
5-9源代碼:
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; //this調用 hireDay = LocalDate.of(year, month, day); } public String getName() // getName方法 { return name; } public double getSalary() //getSalary方法 { return salary; } public LocalDate getHireDay() //getHireDay方法 { return hireDay; } public void raiseSalary(double byPercent) //raiseSalary方法 { 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; //檢測this與otherObject是否引用同一個對象 // must return false if the explicit parameter is null if (otherObject == null) return false; //檢測otherObject是否爲null,若是爲null,返回false // if the classes don't match, they can't be equal if (getClass() != otherObject.getClass()) return false; //比較this與otherObject是否屬於同一個類 // 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() //hashCode方法 { return Objects.hash(name, salary, hireDay); } public String toString() //調用超類的toString方法 { return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]"; } }
5-10源代碼:
package equals; //定義Manager方法 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() //getSalary方法 { double baseSalary = super.getSalary(); return baseSalary + bonus; } public void setBonus(double bonus) //使用setBonus方法 { this.bonus = bonus; } public boolean equals(Object otherObject) //equals方法比較兩個對象是否相同 { 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() //hashCode方法,返回對象的散列碼 { return java.util.Objects.hash(super.hashCode(), bonus); } public String toString() //Manager類中的toString方法,返回描述該對象的字符串 { return super.toString() + "[bonus=" + bonus + "]"; } }
程序運行結果:
測試程序2:
在elipse IDE中調試運行程序5-11(教材182頁),結合程序運行結果理解程序;
掌握ArrayList類的定義及用法;
在程序中相關代碼處添加新知識的註釋;
設計適當的代碼,測試ArrayList類的set()、get()、remove()、size()等方法的用法。
5-11源代碼:
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>(); //將Employee[]數組替換成ArrayList<Employee> //使用add方法將僱員對象添加到數組列表中 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% (把每一個人的工資提升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()); }//循環數組 }
運行結果:
設計適當的代碼,測試ArrayList類的set()、get()、remove()、size()等方法的用法:
package project5; import java.util.ArrayList; import arrayList.Employee; public class myArrayList { public static void main(String[] args) { 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)); //size() int c = staff.size(); System.out.println("ArrayList中存儲的元素個數爲:"+c); for(int i = 0; i<staff.size();i++) { //get() Employee e = staff.get(i); System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); } //set() staff.set(0, new Employee("LAKD",80000,1988,11,23)); Employee e = staff.get(0); System.out.println("修改後爲:name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); //remove() staff.remove(1); System.out.println("Now, Size of list::"+staff.size()); int size1=staff.size(); System.out.println("ArrayList中存儲的元素個數爲:"+size1); for(int i=0; i<staff.size(); i++) { Employee q = staff.get(i); System.out.println("name=" + q.getName() + ",salary=" + q.getSalary() + ",hireDay=" + q.getHireDay()); } for (Employee e1 : staff) e1.raiseSalary(5); for (Employee e1 : staff) System.out.println("name=" + e1.getName() + ",salary=" + e1.getSalary() + ",hireDay=" + e1.getHireDay()); } }
運行結果:
測試程序3:
編輯、編譯、調試運行程序5-12(教材189頁),結合運行結果理解程序;
掌握枚舉類的定義及用法;
在程序中相關代碼處添加新知識的註釋;
刪除程序中Size枚舉類,背錄刪除代碼,在代碼錄入中掌握枚舉類的定義要求。
5-12源代碼:
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 //Size.class是反射,取得Size類 //調用構造函數,並賦值返回枚舉數組的值:Size.SMALL;Size.MEDIUM;Size.LARGE;Size.EXTRA_LARGE 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 _."); in.close(); } } //定義枚舉類型 使用關鍵字enum 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:錄入如下代碼,結合程序運行結果瞭解方法的可變參數用法
代碼:
package project5; 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:編程練習:參照輸出樣例補全程序,使程序輸出結果與輸出樣例一致。
補全後的代碼:
package project5; 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(true); //調用父類 System.out.println("Son's Constructor without parameter"); } public void method() { System.out.println("Son's method()"); super.method(); //調用父類method方法 } }
運行結果:
3. 實驗總結:
這次實驗:
一、調試了private protected public 默認四種修飾符的使用特色,子類擁有父類的全部屬性和方法,但父類的私有屬性和方法,子類是沒法直接訪問到的。即只是擁有,可是沒法使用。二、Object類即全部類的父類,它描述的全部方法子類均可以使用。若是一個類沒有特別指定父類,那麼默認繼承自Object類。實驗中學習了經常使用的API,public String toString():返回該對象的字符串表示。public boolean equals(Object obj):指示其餘某個對象是否與此對象「相等」。三、學習了ArrayList類的定義方法及用途,經過add方法添加元素,int size()返回元素個數…(不太懂得這部分)四、使用enum關鍵字來定義枚舉類,枚舉的構造方法是私有的,因此不能夠new對象,有對象要在它的內部實例化,如enum Size{ SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");} new Size時傳參數爲SMALL("S")。可變參數用法……