再请教大家一个小问题,我就是不懂啊,唉!
import java.io.*;class SuperClass{
int x;
SuperClass( ) {
x=3;
System.out.println("in SuperClass : x=" +x);
}
void doSomething( ) {
System.out.println("in SuperClass.doSomething()");
}
}
class SubClass extends SuperClass {
int x;
SubClass( ) {
super( ); //调用父类的构造方法
x=5; //super( ) 要放在方法中的第一句
System.out.println("in SubClass :x="+x);
}
void doSomething( ) {
super.doSomething( ); //调用父类的方法
System.out.println("in SubClass.doSomething()");
System.out.println("super.x="+super.x+" sub.x="+x);
}
}
public class Inheritance {
public static void main(String args[]) {
SubClass subC=new SubClass();
subC.doSomething();
}
}
他的运行结果是:
in SuperClass: x=3
in SubClass: x=5
in SuperClass.doSomething()
in SubClass.doSomething()
super.x=3 sub.x=5
但按我的理解应该是:
in SuperClass: x=3
in SuperClass.doSomething()
in SubClass: x=5
in SubClass.doSomething()
super.x=3 sub.x=5
我对这个提的哪地方没理解?主类吗?那调用的顺序又是什么?请指教!我小小菜鸟