Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在构造函数内调用此()时是否调用super()_Java_Constructor - Fatal编程技术网

Java 在构造函数内调用此()时是否调用super()

Java 在构造函数内调用此()时是否调用super(),java,constructor,Java,Constructor,我知道构造函数中的第一条语句必须是super()(或this()),如果没有明确指定,编译器将为我们插入它。考虑到这一点,请参见以下代码: class Building { Building() { System.out.print("b "); } Building(String name) { } } public class House extends Building { Hous

我知道构造函数中的第一条语句必须是super()(或this()),如果没有明确指定,编译器将为我们插入它。考虑到这一点,请参见以下代码:

    class Building {
            Building() {  System.out.print("b ");  }
            Building(String name) {

       }
     }
     public class House extends Building {
      House() {  System.out.print("h ");  }
      House(String name) {
        this();   System.out.print("hn " + name);
      }
      public static void main(String[] args) { new House("x "); }
     }

在main方法中,我调用House类的重载构造函数,该构造函数接受一个字符串参数。由于this()已经被调用,这意味着super()被不必要地调用。但是建筑(字符串名)构造函数会发生什么情况?不是也要叫吗?我知道这段代码可以工作并生成
bhhnx
,但是是否需要调用超级类的匹配构造函数呢?

您需要调用一个基类的构造函数,确切地说是一个。在您的示例中,正如您所说的,Building类的无参数构造函数将被调用。将不会调用单参数构造函数


不需要在基类构造函数和派生类构造函数之间匹配参数名称或类型。

您需要调用基类的一个构造函数,并且只调用一个构造函数。在您的示例中,正如您所说的,Building类的无参数构造函数将被调用。将不会调用单参数构造函数


不需要在基类构造函数和派生类构造函数之间匹配参数名称或类型。

让我们在代码的每一行都加上数字,以便更容易解释指针将在何处执行代码:

    1 class Building {
    2     Building() { System.out.print("b ");  }
    3     Building(String name) { } 
    4 }
    5
    6 public class House extends Building {
    7
    8     House() {  System.out.print("h ");  }
    9     House(String name) { this(); System.out.print("hn " + name); }
   10 
   11     public static void main(String[] args) { new House("x "); }
   12 }
当你打电话给新房子(“x”)以下是发生的情况:

/*11*/ new House("x ") --              --> "b h hn x"
                        |             |
                /*9*/ this(); System.out.print("hn " + name); /* House(name) */ 
                        |             ^
                        v             |
                /*8*/ super(); System.out.print("h ");        /* House() */
                        |             ^
                        v             |
                /*2*/ super(); System.out.print("b ");        /* Building() */
                        |             ^
                        v             |
                      super() -------->                       /* Object() */  
            /* The super of Object() does nothing. let's go down. */

and all these calls will print : "b h hn x" as expected :)
每次创建构造函数时,如果不显式调用另一个构造函数,则隐式调用
super()被调用


(在您的例子中,是
Building
)的构造函数。

让我们在代码的每一行都加上数字,以便更容易解释指针将在何处执行代码:

    1 class Building {
    2     Building() { System.out.print("b ");  }
    3     Building(String name) { } 
    4 }
    5
    6 public class House extends Building {
    7
    8     House() {  System.out.print("h ");  }
    9     House(String name) { this(); System.out.print("hn " + name); }
   10 
   11     public static void main(String[] args) { new House("x "); }
   12 }
当你打电话给新房子(“x”)以下是发生的情况:

/*11*/ new House("x ") --              --> "b h hn x"
                        |             |
                /*9*/ this(); System.out.print("hn " + name); /* House(name) */ 
                        |             ^
                        v             |
                /*8*/ super(); System.out.print("h ");        /* House() */
                        |             ^
                        v             |
                /*2*/ super(); System.out.print("b ");        /* Building() */
                        |             ^
                        v             |
                      super() -------->                       /* Object() */  
            /* The super of Object() does nothing. let's go down. */

and all these calls will print : "b h hn x" as expected :)
每次创建构造函数时,如果不显式调用另一个构造函数,则隐式调用
super()被调用


(在您的例子中,是
Building
)的构造函数。

但匹配的构造函数不是:您假设基类中的构造函数通过参数与子类构造函数匹配,
House(String)
将自动调用
Building(String)
?(提示:情况并非如此)。但匹配的构造函数不是这样的:假设基类中的构造函数通过参数与子类构造函数匹配,
House(String)
将自动调用
Building(String)
?(提示:事实并非如此)。好的,那么,如果房屋(字符串名)构造函数中没有调用this(),那么会隐式调用哪个建筑构造函数?无参数的那个。如果需要调用带有参数的基类的构造函数,则始终需要显式调用
super()
。@MikeJM它将调用父类hmm的默认无参数构造函数,我知道了。因此,隐式调用只对无参数构造函数进行,并且基类和子类的签名之间绝对没有关系。我说的对吗?好的,那么如果房子(字符串名)构造函数中没有调用this(),那么会隐式调用哪个建筑构造函数呢?无参数的那个。如果需要调用带有参数的基类的构造函数,则始终需要显式调用
super()
。@MikeJM它将调用父类hmm的默认无参数构造函数,我知道了。因此,隐式调用只对无参数构造函数进行,并且基类和子类的签名之间绝对没有关系。我说的对吗?谢谢你的草图。谢谢你的草图。