标签:
源代码1:
class Base{ public Base(){ System.out.println("Base类中的构造器"); } } class Base_Two extends Base{ public Base_Two(){ System.out.println("Base_Two类中的构造器"); } } class Base_Three extends Base_Two{ public Base_Three(){ System.out.println("Base_Three的构造器"); } } public class Java7 { public static void main(String[] args) { Base_Three ac=new Base_Three(); } }
源代码2:
class Base{ public String name; public String sex; public Base(String name,String sex){ this.name=name; this.sex=sex; } } class Base_Two extends Base{ public String age; public Base_Two(String name,String sex,String age){ super(name,sex); this.age=age; } } public class Java8 { public static void main(String[] args) { Base_Two ac=new Base_Two("xiaoming","man","19"); System.out.println(ac.name+" "+ac.sex+" "+ac.age); } }运行结果:
1.由源代码1可以知道继承中虽然不会继承构造器,但是继承时是会把所有基类的构造器执行一遍;
2.由源代码2可知:继承中引用基类的构造器用关键字super,同一类中构造器的重载引用其他构造器用语句this;
3.即使父类中没有构造器,子类也会隐式地调用父类默认的无参数构造器;
标签:
原文地址:http://blog.csdn.net/hcy2319964421/article/details/51334119