標籤:類繼承 簡單工廠設計模式 dog inf interface 主函數 繼承、抽象類別 表示 png
一、學習要點
認真看書並查閱相關資料,掌握以下內容:
掌握類的繼承概念和設計
掌握構造方法的繼承原則
掌握方法重寫
掌握super鍵字和final關鍵字
掌握對象轉型,理解通過向上轉型實現對象多態。
掌握抽象類別的設計
掌握介面的設計
掌握介面回調
理解簡單工廠設計模式
理解抽象類別和介面的區別
掌握封裝類的應用
掌握對象的比較方法和比較子的使用
學習使用日期操作類
學習匿名內部類的使用
二、作業要求
發布一篇隨筆,主要包括以下幾部分的內容:
(一)學習總結
1.閱讀下面程式,分析是否能編譯通過?如果不能,說明原因。應該如何修改?程式的運行結果是什嗎?為什麼子類的構造方法在運行之前,必須調用父 類的構造方法?能不能反過來?
~~~class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); } public Grandparent(String string) { System.out.println("GrandParent Created.String:" + string); }}class Parent extends Grandparent { public Parent() { System.out.println("Parent Created"); super("Hello.Grandparent."); }}class Child extends Parent { public Child() { System.out.println("Child Created"); }}public class Test{ public static void main(String args[]) { Child c = new Child(); }}~~~
不能通過編譯。super調用的是父類的建構函式,super關鍵字必須在子類構造方法的前面。
運行結果:
GrandParent Created.String:Hello.Grandparent.
Parent Created
Child Created
先初始化父類的構造方法變數與屬性,才能執行個體化子類,不能反過來。
2.閱讀下面程式,剖析器中存在哪些錯誤,說明原因,應如何改正?正確程式的運行結果是什嗎?
class Animal{ void shout(){ System.out.println("動物叫!"); }}class Dog extends Animal{ public void shout(){ System.out.println("汪汪......!"); } public void sleep() { System.out.println("狗狗睡覺......"); } }public class Test{ public static void main(String args[]) { Animal animal = new Dog(); animal.shout(); animal.sleep(); Dog dog = animal; dog.sleep(); Animal animal2 = new Animal(); dog = (Dog)animal2; dog.shout(); }}
向上轉型只能不能調用子類新定義的方法
改為
Dog dog = (Dog)animal;
dog.sleep();
汪汪......!
狗狗睡覺......
狗狗睡覺......
汪汪......!
3.運行下列程式
class Person { private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } }public class Test{ public static void main(String args[]){ Person per = new Person("張三",20) ; System.out.println(per); System.out.println(per.toString()) ; } }
(1)程式的運行結果如下,說明什麼問題?
[email protected]
[email protected]
per與per.toString()輸出的均為目標的地址
(2)那麼,程式的運行結果到底是什麼呢?利用eclipse開啟println(per)方法的源碼,查看該方法中又調用了哪些方法,能否解釋本例的運行結果?
···
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
···
(3)在Person類中增加如下方法
public String toString(){ return "姓名:" + this.name + ",年齡:" + this.age ; } ```重新運行程式,程式的執行結果是什嗎?說明什麼問題?運行結果:姓名:張三,年齡:20姓名:張三,年齡:20說明了在沒有寫toSting方法時,預設的toString方法是object中的toString方法4.汽車租賃公司,出租汽車種類有客車、貨車和皮卡三種,每輛汽車除了具有編號、名稱、租金三個基本屬性之外,客車有載客量,貨車有載貨量,皮卡則同時具有載客量和載貨量。用物件導向編程思想分析上述問題,將其表示成合適的類、抽象類別或介面,說明設計思路。現在要建立一個可租車列表,應當如何建立?建立一個汽車類,分別建立客車,貨車,皮卡類去繼承汽車類建立兩個介面載客介面與載貨介面由三個子類繼承。5.閱讀下面程式,分析代碼是否能編譯通過,如果不能,說明原因,並進行改正。如果能,列出運行結果
interface Animal{ void breathe(); void run(); void eat();}class Dog implements Animal{ public void breathe(){ System.out.println("I‘m breathing"); } void eat(){ System.out.println("I‘m eating"); }}public class Test{ public static void main(String[] args){ Dog dog = new Dog(); dog.breathe(); dog.eat(); }}
```
不能:介面中的方法預設由public abstract修飾 所以在子類實現抽象方法時,應該用public修飾
並且需要實現介面中所有的抽象方法
6.其他需要總結的內容。
(二)實驗總結
本次實驗包括實驗四和實驗五兩次的內容:
對完成實驗內容過程中遇到的問題、解決方案以及程式的設計思路和思考等進行歸納總結。
1 銀行
建立銀行類,定義相關方法與對象,主函數中while迴圈與switch語句實現菜單功能
2員工
建立員工類 ,由經理類與職員類繼承
3圖形計算
建立一個平面圖形的抽象類別,再建立一個立體圖形的抽象類別,不同的圖形繼承不同的抽象類別並重寫相關方法。在測設類中隨機產生圖形並用隨機數來產生圖形的相關資料。
(三)代碼託管(務必連結到你的項目)
《Java技術》第三次作業--物件導向——繼承、抽象類別、介面