根据我对构造函数链接的理解,
每当我们创建子类的对象(或调用子类构造函数)时,仅
当 我们的子构造函数没有碰巧使用this(对于同一类)或super关键字调用另一个构造函数时,才会首先自动调用父类的默认构造函数. 来源:http ://www.java67.com/2012/12/how-constructor-chaining-works-in-java.html
所以如果我的理解是正确的
然后对于以下代码:-
Class First{
First(){
System.out.print("Hello");
}
Class Second extends First{
Second(int i)
{
System.out.println("Blogger");
}
Second(){
this(2); //default constructor is calling another constructor using this keyword
}
public static void main(String[] args)
{
Second ob = new Second();
}
输出应该Blogger
只有。
但输出是HelloBlogger
因此,似乎确实仍在调用父类的默认构造函数。但是引用该来源:-
2)如果您不从父类或同一类调用另一个构造函数,而不是Java调用默认或超类的无参数构造函数。
阅读更多:http ://www.java67.com/2012/12/how-constructor-chaining-works-in-java.html#ixzz4qztuMrKW
所以请帮忙!