Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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(),那么在创建子类的对象时,这个super()将调用哪个类?_Java_Loops_Inheritance - Fatal编程技术网

Java 如果我们在父类构造函数中添加super(),那么在创建子类的对象时,这个super()将调用哪个类?

Java 如果我们在父类构造函数中添加super(),那么在创建子类的对象时,这个super()将调用哪个类?,java,loops,inheritance,Java,Loops,Inheritance,在下面的示例中,super()将在创建方形对象时调用形状构造函数中的哪个类 package com.company; class Shape { public Shape() { super(); System.out.println("inside Shape class default constructor"); } } class Rectangle extends Shape { public Rectangle() {

在下面的示例中,super()将在创建方形对象时调用形状构造函数中的哪个类

package com.company;

class Shape {
    public Shape() {
        super();
        System.out.println("inside Shape class default constructor");
    }
}

class Rectangle extends Shape {
    public Rectangle() {
        super();
        System.out.println("inside Rectangle class default constructor");
    }
}

class Square extends Rectangle {
    public Square() {
        super();
        System.out.println("inside Square class default constructor");
    }
}

public class Ex2 {
    public static void main(String[] args) {
        Square sq = new Square();
    }
}

它将调用
java.lang.Object
类的构造函数,它是java中所有类的默认父类。

它将调用
java.lang.Object
类的构造函数,它是java中所有类的默认父类。

回答得好,“但请不要鼓励OP期望它成为家庭作业资源。”物理学家,我认为这是一个好问题。可能有一个副本给我,但我找不到。@KarthikeyanVaithilingam我找到了它,并将它列在question@MadPhysicist是的,我现在看到了,我猜页面没有刷新。回答得好,但请不要鼓励OP期望它成为家庭作业资源。@MadPhysician我认为这是一个好问题。可能有一个副本给我,但我找不到。@KarthikeyanVaithilingam我找到了它,并将它列在question@MadPhysicist是的,我现在可以看到,我猜页面没有刷新。这是副本:这是副本: