《Java技術》第三次作業--物件導向——繼承、抽象類別、介面

來源:互聯網
上載者:User

標籤:bubuko   構造方法   定義   寵物商店   cli   anim   需要   分享   string   

一)學習總結

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語句必須放在子類構造方法的首行。將改語句放在子類構造方法的首行即可。

運行結果為

先調用父類的構造方法為父類中的屬性初始化,就表示先有父類執行個體,才有子類執行個體,不能反過來。

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();    }}

存在的錯誤:
1、父類向上轉型申明的屬性,子類只能用其調用父類中含有的方法;
2、向下轉型時,必須確認子類和父類存在關係
改正後

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()) ;   } }

運行結果為

)那麼,程式的運行結果到底是什麼呢?利用eclipse開啟println(per)方法的源碼,查看該方法中又調用了哪些方法,能否解釋本例的運行結果?

print方法中調用了write方法,列印了類本身。
(3)在Person類中增加如下方法

public String toString(){         return "姓名:" + this.name + ",年齡:" + this.age ;  } 

修改後

class Person {    private String name ;    private int age ;    public Person(String name,int age){          this.name = name ;          this.age = age ;    }    public String toString(){        return "姓名:" + this.name + ",年齡:" + this.age ; } }public class Test{        public static void main(String args[]){              Person per = new Person("張三",20) ;              System.out.println(per);             System.out.println(per.toString()) ;   } }

運行結果

覆寫了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修飾
並且需要實現介面中所有的抽象方法,這裡需要再實習介面中的run方法。
(二)實驗總結
1、寵物商店中,總是不能將需要輸入的東西傳到數組中,不能很好的寫出相關代碼;
2、申明數組後,必須對數組內每個成員進行執行個體化,否則會出現null 指標現象
3、繼承必須對父類所有方法進行重寫。
4、上轉型

對象轉型

上轉型對象

上轉型對象:使父類對象引用指向一個子類執行個體
上轉型格式:父類名 父類對象=new 子類名(子類執行個體);
上轉型的叫法:父類對象xxx是子類對象的上轉型對象

上轉型對象的特點:

上專性對象不能對子類新添加的變數或方法進行操作
上轉型對象調用的方法只能是子類重寫過的方法以及繼承且沒有修改的方法
上轉型對象可以調用子類繼承或隱藏的變數

(三)代碼託管(務必連結到你的項目)

https://gitee.com/hebau_java_cs16/Java_CS01ZY/tree/master
碼雲提交曆史

《Java技術》第三次作業--物件導向——繼承、抽象類別、介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.