Java构造方法的继承调用
1.对于父类含有参数构造函数的方法,子类可以通过在自己构造方法中使用super关键字来调用,但是super必须是第一个可执行语句
输出:
Father x=1
Son x=1
Son Sub_1
Son Sub_1 x=2
2.父类无参构造函数一定被调用
class Father{ public Father(){ System.out.println("FatherGouzao"); } public Father(int x){ System.out.println("Father x="+ x); } public void Sub_1() { System.out.println("Father Sub_1"); } } public class Son extends Father{ public Son(){ System.out.println("SonGouzao"); } public Son(int x){ super(x); System.out.println("Son x="+ x); } public void Sub_1(){ System.out.println("Son Sub_1"); } public void Sub_1(int x){ System.out.println("Son Sub_1 x="+x); } public static void main(String[] args) { // TODO Auto-generated method stub Son Object = new Son(1); Object.Sub_1(); Object.Sub_1(2); } }
输出:
Father x=1
Son x=1
Son Sub_1
Son Sub_1 x=2