java第三次作業

來源:互聯網
上載者:User

標籤:...   利用   強制   str   託管   如何   覆寫   編程   思路   

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

不能通過,parent類裡面的super("Hello.Grandparent.")沒有放在第一行,把它放在第一行就行了。
結果為
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();    }}

animal.sleep() 和 Dog dog=animal 有錯
animal.sleep()是因為上轉型後Dog類裡面的sleep方法並不是繼承或者重寫的,所以有錯,刪除這個方法就行了。
Dog dog=animal 是因為向下轉型時候應該強制類型轉換,應該為Dog dog=(Dog)animal
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]
Person類裡面沒有寫tostring方法,測試類別裡面就直接調用了。
(2)那麼,程式的運行結果到底是什麼呢?利用eclipse開啟println(per)方法的源碼,查看該方法中又調用了哪些方法,能否解釋本例的運行結果?

(3)在Person類中增加如下方法

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

重新運行程式,程式的執行結果是什嗎?說明什麼問題?
程式執行結果:
姓名:張三,年齡:20
姓名:張三,年齡:20
新增加的tostring方法覆寫了Person類繼承的方法,輸出的是新增加的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();        }    }

不能編譯通過,1.Dog類裡面的eat方法應該有public
2.Dog類應該覆寫所有父類的方法,run()方法沒有覆寫。
(二)實驗總結
排序時,可以用gettime進行比較。
(三)代碼託管
https://gitee.com/deep_tenderness/hebau_cs01WSS/tree/master/ex04

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.