"SCJP 1.6版考題 014" 修訂間的差異
出自 陳富國維基館
(新頁面: 11. class Person{ 12. String name = "No name"; 13. public Person(String nm){name = nm;} 14. } 15. 16. class Employee extends Person{ 17. String empID = "0000"; 18. publ...) |
(無差異)
|
於 2013年3月30日 (六) 08:47 的最新修訂
11. class Person{
12. String name = "No name";
13. public Person(String nm){name = nm;}
14. }
15.
16. class Employee extends Person{
17. String empID = "0000";
18. public Employee(String id){empID = id;}
19. }
20.
21. class EmployeeTest{
22. public static void main(String[] args){
23. Employee e = new Employee("4321");
24. System.out.println(e.empID);
25. }
26. }
What is the result?
A. 4321
B. 0000
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 18.
解答
Ans: D
解說:
下面二個方法以第2種方法較好,記得有一個黃金定律,一個類別裏,不管如何,都要有一個無參數的建構子…!
1.
class Employee extends Person{
String empID = "0000";
public Employee(String id){super(id); empID = id;}
}
2.
class Person{
String name = "No name";
public Person(String nm){name = nm;}
public Person(){}
}
| ||||||||||||||||||||
Ans:D 解說: