public class TestExtend {
class A {
protected void print(String s){
System.out.println(s);
}
A() {print("A()");}
public void f() {print("A:f()");}
}
class B extends A {
B() {print("B()");}
public void f() {print("B:()");}
}
public static void main (String[] args) {
B b = new B(); --錯誤行,這行代碼有問題
b.f();
}
}
靜態內部類的用法,你想要在類的靜態方法中使用內部類的話,那麼需要將內部類定義成靜態的,不然是找不到內部類的,因為不聲明static的話,內部類只會在TestExtend對象創建之後才會生成。你可以試一下如下的代碼:
public class TestExtend {
static class A {
protected void print(String s){
System.out.println(s);
}
A() {print("A()");}
public void f() {print("A:f()");}
}
static class B extends A {
B() {print("B()");}
public void f() {print("B:()");}
}
public static void main (String[] args) {
B b = new B();
b.f();
}
}