静态方法不能被重写,非静态方法才能被子类重写。
public class Person { public static void f1(){ System.out.println("person static f1"); } public void f2(){ System.out.println("person f2"); } }
public class Student extends Person{ public static void f1(){ System.out.println("student static f1"); } public void f2() { System.out.println("student f2"); } }
public class Test { public static void main(String[] args) { Student student = new Student(); student.f1(); student.f2(); System.out.println("-----------------------"); Person person = new Student(); person.f1(); person.f2(); } }
上述代码结果如下