为什么在编译时出错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