父类中必须有不带参数的构造方法,否则就在子类的构造方法中的第一行加super()显示的继承父类构造方法
package day07; class Fu { int num =9; Fu() { System.out.println("fu run"+num); } Fu(int x) { x=3; System.out.println("fu run "+x); } } class Zi1 extends Fu { Zi1() { System.out.println("Zi1 run"); } Zi1(int num) { this();//调用本类构造函数 System.out.println("Zi1 run"+num); } } public class Zi { public static void main(String[] args) { Fu f = new Zi1(6); Zi1 z = new Zi1(6); } }
fu run9
Zi1 run
Zi1 run6
fu run9
Zi1 run
Zi1 run6
原文:http://www.cnblogs.com/mmlovejj/p/5083754.html