Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

JAVA考古題小困惑

Which two classes correctly implement both the java.lang.Runnable

and the java.lang.Clonable interfaces? (Choose two.)

A. public class Session

implements Runnable, Clonable {

public void run();

public Object clone();

}

B. public class Session

extends Runnable, Clonable {

public void run() { / do something */ }

public Object clone() { / make a copy */ }

}

C. public class Session

implements Runnable, Clonable {

public void run() { / do something */ }

public Object clone() { /* make a copy */ }

}

D. public abstract class Session

implements Runnable, Clonable {

public void run() { / do something */ }

public Object clone() { /*make a copy */ }

}

E. public class Session

implements Runnable, implements Clonable {

public void run() { / do something */ }

public Object clone() { / make a copy */ }

}

不懂為什麼是選CD而不是AB呢@@?

Given:

11. public class Ball {

12. public enum Color { RED, GREEN, BLUE };

13. public void foo() {

14. // insert code here

15. { System.out.println(c); }

16. }

17. }

Which code inserted at line 14 causes the foo method to print RED,

GREEN, and BLUE?

A. for( Color c : Color.values())

B. for( Color c = RED; c <= BLUE; c++)

C. for( Color c; c.hasNext() ; c.next())

D. for( Color c = Color[0]; c <= Color[2]; c++)

E. for( Color c = Color.RED; c <= Color.BLUE; c++)

為什麼答案會是A @@?

而不是其他答案???

麻煩大大講解了

1 Answer

Rating
  • 1 decade ago
    Best Answer

    1. 題目有明確指示要 implement both interfaces,所以答案B中用extends是不合的;而答案A中,既然己經 implements 了,則 run() 和 clone() 這兩個methods 就要實作,也就是要有內容(像B, C, D, E那樣)。

    2. 簡單來說,enum 是一種特殊的類別,因此他的用法也與一般不同。在答案A中,values() 這個方法會傳回一個陣列,裡頭包含所有 Color 裡宣告的成員,所以是合法的。

Still have questions? Get your answers by asking now.