对于Java中的以下代码,我使用Person u = new Undergrad();
u.method1();
运行它。
如果我想在Student类中调用method2而不是在Undergrad类中调用method2,我应该如何更改Student类中的method1以在同一个类中调用method2?
public class Person {
public void method1() {
System.out.print("Person 1 ");
}
public void method2() {
System.out.print("Person 2 ");
}
}
public class Student extends Person {
public void method1() {
System.out.print("Student 1 ");
super.method1();
method2();
}
public void method2() {
System.out.print("Student 2 ");
}
}
public class Undergrad extends Student {
public void method2() {
System.out.print("Undergrad 2 ");
}
}