我發現了這個javatpoint的代碼。但是當我嘗試編譯它時,它給了我一個錯誤。分層繼承
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
No enclosing instance of type Hierarchical is accessible. Must qualify the allocation with an enclosing instance of type Hierarchical (e.g. x.new A() where x is an instance of Hierarchical).
No enclosing instance of type Hierarchical is accessible. Must qualify the allocation with an enclosing instance of type Hierarchical (e.g. x.new A() where x is an instance of Hierarchical).
at Inheritance.Hierarchical.main(Hierarchical.java:28)
爲什麼我得到這個錯誤? Eclipse
告訴我讓類是靜態的,但我不明白爲什麼?
package Inheritance;
public class Hierarchical {
static void bayern() {
System.out.println("bayern");
}
class Hierarchical2 extends Hierarchical {
static void barcelona() {
System.out.println("barcelona");
}
}
class Hierarchical3 extends Hierarchical {
static void Madrid() {
System.out.println("madrid");
}
}
public static void main(String[] args) {
Hierarchical2 h2 = new Hierarchical2();
Hierarchical3 h3 = new Hierarchical3();
h2.barcelona();
h2.bayern();
h3.Madrid();
h3.bayern();
}
}
[Java內部類和靜態嵌套類]的可能重複(http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) –