2017Java技術第四次做業

(一)學習總結

1.學習使用思惟導圖對Java面向對象編程的知識點(封裝、繼承和多態)進行總結。

2.閱讀下面程序,分析是否能編譯經過?若是不能,說明緣由。應該如何修改?程序的運行結果是什麼?爲何子類的構造方法在運行以前,必須調用父 類的構造方法?能不能反過來?

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

不能經過編譯,構造函數調用必須是構造函數中的第一個語句。java

class Parent extends Grandparent {
    public Parent() { 
    super("Hello.Grandparent.");
    System.out.println("Parent Created");
}
}

運行結果爲:編程

GrandParent 
Created.String:Hello.Grandparent.
Parent Created
Child Created`

子類的構造方法必須先調用父類構造,再執行子類構造方法。eclipse

3 . 閱讀下面程序,分析程序中存在哪些錯誤,說明緣由,應如何改正?正確程序的運行結果是什麼?

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.sleep()使用方法是在子類中新定義的,sleep中沒有考慮到父類,應該在父類中覆寫,不能夠直接使用子類的方法。
2.將(Dog)animal將父類轉爲子類便可。
更改:函數

class Animal{
void shout(){
    System.out.println("動物叫!");
}
void sleep(){     
    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 = (Dog)animal;
        dog.sleep(); 
        Animal animal2 = new Dog();    
        dog = (Dog)animal2;
        dog.shout();
    }
    }

運行結果:學習

汪汪......!
狗狗睡覺......
狗狗睡覺......
汪汪......!

4.運行下列程序

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)程序的運行結果以下,說明什麼問題?測試

Person@166afb3
Person@166afb3

在Person類中沒有定義方法toString(),最後定不定義結果同樣。this

(2)那麼,程序的運行結果究竟是什麼呢?利用eclipse打開println(per)方法的源碼,查看該方法中又調用了哪些方法,可否解釋本例的運行結果?設計

public void println(Object x) {
    String s = String.valueOf(x);
    synchronized (this) {
        print(s);
        newLine();
    }
    }

(3)在Person類中增長以下方法code

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

從新運行程序,程序的執行結果是什麼?說明什麼問題?
運行結果:
姓名:張三,年齡:20
姓名:張三,年齡:20
父類中的toString()方法覆蓋了子類中的方法。對象

(二)實驗總結

實驗內容:

1.定義員工類,具備姓名、年齡、性別屬性,並具備構造方法和顯示數據方法。定義管理層類,繼承員工類,有本身的屬性職務和年薪。定義職員類,繼承員工類,並有本身的屬性所屬部門和月薪。定義一個測試類,進行測試。畫出類圖。

程序設計思路:設計一個員工類,一個管理員類,一個職工類,一個測試類。管理員類和職工類分別繼承員工類。

相關文章
相關標籤/搜索