Examly題庫立即開始練習
原住民族考試四等考試-電子工程類科計算機概要10524單選題

下列的 Java 語言程式執行後輸出為何? import java.lang.*; class A { public int fun(){ return 1; } } class B extends A { public int fun(){ return 2; } } class C extends A { public int fun(){ return 3; } } class test { public static void main (String[] argv) { A[] a={new A(), new B(), new C()}; int x=0; for(int i=0;i<a.length;i++) x+=a[i].fun(); System.out.println(x); } }

A3
B5
C6正確答案
D9
答案與詳解
C
正確答案
Java 多型 (Polymorphism):動態繫結呼叫各物件實際的 fun(),1+2+3=6。

為什麼答案是 C

陣列三物件分別為 A、B、C 實體,因動態繫結分別呼叫自己的 fun(),回傳 1、2、3,累加得 6。

考點:忽略累加考點:多型誤解考點:動態繫結考點:執行誤算
載入中…

計算機概要 相關題目

想練更多計算機概要考古題?

Examly 收錄 38 萬+ 道歷屆題目,每題都有像這樣的精選詳解。免費下載,立即開練。

Download on theApp Store即將推出Google Play
黑皮