close

public class Outside {
static String out_a = "Outside class string 1";
String out_b = "Outside class string 2";
public static void main(String[] args) {
Inner1 a = new Outside().new Inner1();
Outside.Inner1 b = new Outside().new Inner1();
Inner1 c = new Inner1(); //錯誤,因Inner1 是動態故需先建立他所在的class Outside 之實體
Inner2 d = new Outside().new Inner2(); // error
Outside.Inner2 e = new Outside().new Inner2(); //錯誤,此語法只適用在動態內部類別
Inner2 f = new Inner2(); //,因Inner2 是靜態故不需先建立他所在的class Outside 之實體
Inner1 g =this.new Inner1(); // 錯誤, this 或super 指令不能出現在靜態範圍裡
System.out.println(Outside.Inner2.inner2_a);
}
Outside( ) {
Inner1 a = new Inner1( );
Inner1 b = this.new Inner1( ); // this.後不能接new 指令
Inner2 c = new Inner2( );
Inner2 d = new Outside.Inner2();
Inner2 e = this.new Inner2( ); // this.後不能接new 指令
}
class Inner1 { //動態內部類別
String inner1_a = out_a;
static String inner1_a = out_a; // 錯誤, 因動態內部類別內不能有靜態宣告
String inner1_b = out_b;
String inner1_c = Inner2.inner2_a;
String inner1_d = Inner2.inner2_b; // 錯誤, inner2_b 為動態須先建立Inner2 實體.
}
static class Inner2 { //靜態內部類別
static String inner2_a = out_a;
String inner2_b = out_a;
String inner2_c = out_b; // 靜態內部類別不能讀取外部類別之動態屬性
}
}
-

 

考題InnerClass:

(14) Click the exhibit button:
1. public class EnclosingOne {
2. public class InsideOne {}
3. }
4. public class inertest {
5. public static void main(String[] args){
6. EnclosingOne eo= new EnclosingOne();
7. //insert code here
8. }
9. }

Which statement at line 7 constructs an instance of the inner class?

constructs 建立


A. InsideOne ei= eo.new InsideOne();
B. eo.InsideOne ei = eo.new InsideOne();
C. InsideOne ei = EnclosingOne.new InsideOne();
D. EnclosingOne.InsideOne ei = eo.new InsideOne();

答案: D, A等號左邊找不到InnerA; B等號左邊錯,:C兩邊都錯

 

考題InnerClass:

 

(131) Given:
1. public class OuterClass {
2. private double d1 = 1.0;
3. //insert code here
4. }

You need to insert an inner class declaration at line3. Which two inner class declarations are valid? (Choose Two)

line 3 第3行
declaration 宣告
valid 合法


A. static class InnerOne { public double methoda(){return d1;} }
B. static class InnerOne { static double methoda(){return d1;} }
C. private class InnerOne { public double methoda(){return d1;} }
D. protected class InnerOne { static double methoda(){return d1;} }
E. public abstract class InnerOne { public abstract double methoda(); }

答案: C,E

 

 

1. interface Foo {
2. void foo();
3. }
4.
5. public class Test implements Foo {
6. public static void main(String argv[]){
7. Foo a = new Test();
8. a.foo();
9. }
10. protected void foo(){
11. System.out.println("foo");
12. }
13. }

What’s the result ?


A. compile and output "foo".
B. compile error at line 2.
C. compile error at line 7.
D. compile error at line 10.

答案: D, 地10行是為了覆寫第2行的method.第2行的 void foo(); 前面自動會加public static final, 所以要覆寫他的實作第10行,存取權限必須是比較大或一樣,例如"public"。

 

考題Interface:

(15) Click the exhibit button:
1. interface Foo {
2. int k = 0;
3. }
4.
5. public class Test implements Foo {
6. public static void main(String args[]){
7. int i;
8. Test test = new Test ();
9. i= test.k;
10. i= Test.k;
11. i= Foo.k;
12. }
13. }

What is the result?

 

A. Compilation succeeds.
B. An error at line 2 causes compilation to fail.
C. An error at line 9 causes compilation to fail.
D. An error at line 10 causes compilation to fail.
E. An error at line 11 causes compilation to fail.

答案: A, CDE沒有問題, 最佳寫法是E。B沒有問題,

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 鈴木保齡球 的頭像
    鈴木保齡球

    Java程式學習手札

    鈴木保齡球 發表在 痞客邦 留言(0) 人氣()