子类对象实例化面试题,拿来给大家分享下了,希望指点哦
package com.gem.j2SE03;
abstract class A {
private int x = 100;
public A(){
this.print();
}
public abstract void print();
}
class B extends A {
private int x = 200;
public B(int x){
this.x = x;
}
public void print(){
System.out.println("x=" + x);
}
}
public class TestJava
{
private void mian()
{
A a = new B(20);
}
}
答案:x=0
子类的实例化过程:应该是先调用父类中的构造方法,然后再执行子类中的自己的构造方法了,查了资料才看到的,学了不少哦