class person { string name = “person”; Public void shout() { System.out.print(name); } } class student extends person{ string name = “student”; string school = “school”; } class Test{ public static void main(string[] args){ person p = new student(); p.shout(); } }
答案是person
在父类new子类时,如果子类没有重写将要调用的方法,那么调用的是父类的方法,并且如果该方法有变量时,该变量以就近原则来赋值。如果子类中重写,就调用子类的方法。