Write the execution result of object-oriented code with inheritance relationship

 寫出繼承關係的物件導向程式碼之執行結果

就物件導向程式而言,現有如下的繼承關係的類別。(15 分)

class GrandFather

{ String name = "GrandFather";

String getName() { return name; }

String whereIam() { return "class GrandFather"; }

} // End of GrandFather class

class Father extends GrandFather

{ String getName() { return name; }

String whereIam() { return "class Father"; }

} // End of Father class

class Son extends Father

{ String name = "Son";

String getName() { return name; }

String whereIam() { return "class Son"; }

} // End of Son class

class Daughter extends Father

{ String getName() { return name; }

String whereIam() { return "class Daughter"; }

} // End of Daughter class

於其他類別方法中使用如下的 statement:

Father f1 = new GrandFather();

Father f2 = new Son();

Father f3 = new Daughter();

System.out.println(f2.getName());

System.out.println(f3.getName());

System.out.println(f2.whereIam());

System.out.println(f3.whereIam());

請問編譯執行後,會顯示什麼結果?如為語法有誤或執行時期發生中斷,請說明原

因,並將該 statement 予以忽視,然後說明可以正確執行 statement 所產生的結果

答案

(一)
將上述程式碼轉類別圖,能清楚的看出類別之間的關係


(二)

Father f1 = new GrandFather();

Father f2 = new Son();

Father f3 = new Daughter();

會發生型別錯誤,正確的敘述如下

GrandFather f1 = new GrandFather();

Son f2 = new Son();

Daughter f3 = new Daughter();

最後會印出下列結果
Father f1 = new GrandFather();
f1變數實際類別實體為GrandFather類別
Father f2 = new Son();
f2變數實際類別實體為Son類別
Father f3 = new Daughter();
f3變數實際類別實體為Daughter類別
System.out.println(f2.getName());
故輸出為Son類別定義的name 屬性,印出Son
System.out.println(f3.getName());
Daughter 類別沒有定義name 屬性往父類別找。
父類別Father 類別沒有定義name 屬性往父類別找。
父類別GrandFather類別有定義name 屬性,故會印出GrandFather
System.out.println(f2.whereIam());
由Son 類別定義的方法得回傳值"class Son"印出。
System.out.println(f3.whereIam())
由Daughter 類別定義的方法得回傳值"class Daughter"印出。

Chatgpt 的答案

這個程式中存在多個類之間的繼承關係,並且宣告和初始化了幾個不同類型的物件。以下是對提供的程式碼的說明:


```java

class GrandFather

{

    String name = "GrandFather";

    String getName() { return name; }

    String whereIam() { return "class GrandFather"; }

}


class Father extends GrandFather

{

    String getName() { return name; }

    String whereIam() { return "class Father"; }

}


class Son extends Father

{

    String name = "Son";

    String getName() { return name; }

    String whereIam() { return "class Son"; }

}


class Daughter extends Father

{

    String getName() { return name; }

    String whereIam() { return "class Daughter"; }

}

```


在這些類中,`GrandFather`是頂層類別,`Father`繼承自`GrandFather`,`Son`和`Daughter`則繼承自`Father`。每個子類都重寫了`getName`和`whereIam`方法,並且`Son`和`Daughter`中有它們自己的`name`成員變數。


現在來看以下程式:


```java

Father f1 = new GrandFather();

Father f2 = new Son();

Father f3 = new Daughter();

System.out.println(f2.getName());

System.out.println(f3.getName());

System.out.println(f2.whereIam());

System.out.println(f3.whereIam());

```


1. `Father f1 = new GrandFather();` 創建一個`Father`型別的物件,但實際上初始化為`GrandFather`。由於`Father`繼承自`GrandFather`,所以這是合法的。


2. `Father f2 = new Son();` 創建一個`Father`型別的物件,但實際上初始化為`Son`。由於`Son`繼承自`Father`,所以這也是合法的。


3. `Father f3 = new Daughter();` 創建一個`Father`型別的物件,但實際上初始化為`Daughter`。同樣,這是合法的。


接下來,針對`f2`和`f3`的`getName`和`whereIam`方法的呼叫會根據物件的實際類型執行,因為這是多態性的一個特性。結果如下:


- `f2.getName()` 呼叫`Son`類別的`getName`方法,所以會印出`Son`。


- `f3.getName()` 呼叫`Daughter`類別的`getName`方法,所以會印出`Daughter`。


- `f2.whereIam()` 呼叫`Son`類別的`whereIam`方法,所以會印出`class Son`。


- `f3.whereIam()` 呼叫`Daughter`類別的`whereIam`方法,所以會印出`class Daughter`。


結果總結:

```

Son

Daughter

class Son

class Daughter

```


程式正確執行並顯示這些結果。


Comments

Popular posts from this blog

How to write data into a excel file using vbscript

Format date as yyyy-mm-dd using vbscript