class MyTest2 {
public static void main(String[] args) {
A a = new B();//对子类B进行初始化,那么先要对父类A进行初始化
a.show();//编译看左边,运行看右边
a.hehe();
System.out.println(a.AA);//调用变量时,编译运行都看左边
System.out.println(a.num);
}
}
abstract class A {
int num = 20;
public final int AA = 20;
public A() {
System.out.println("抽象类的空参构造执行了");
}
public abstract void show();
public void hehe() {
System.out.println("hehe");
}
}
class B extends A {
int num = 600;
public B() {
System.out.println("子类的空参构造执行了");
}
@Override
public void show() {
System.out.println("子类show");
}
@Override
public void hehe() {
System.out.println("子类hehe");
}
}
运行结果为:
抽象类的空参构造执行了
子类的空参构造执行了
子类show
子类hehe
20
20