爲什麼在編譯時出錯if (i == 0) { print(); }
?是否因爲main是靜態的,即使它在A的類中?java中的靜態方法main
public class A {
private void print() { System.out.print(foo() + " "); }
public String foo() { return "AAA"; }
public static void main(String[] args) {
A[] arr = { new A(), new B() };
for (int i = 0; i < 2; i++) {
/***/ if (i == 0) { print(); }
}
}
}
public class B extends A {
private void print() { System.out.println("%" + foo() + " "); }
public String foo() { return "BBB"; }
public void bar() { print(); }
}
main is static;打印不是。非常基本的東西;說實話,你不應該要求;但要學習教程,書籍等。 – GhostCat
瞭解什麼'static'意思是:[瞭解類成員](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) – Jesper