构造函数调用

发布于 2024-12-05 16:11:57 字数 528 浏览 0 评论 0原文

我有三个类:

public class A {
    public A(){
        System.out.println("in A");
    }
}


public class B extends A{

    public B(){
        System.out.println("in b");
    }
} 


public class C extends B{

    public C(){
        System.out.println("in C");
    }   
}

现在我真的不确定构造函数调用是如何工作的。 如果我实例化 C c= new C();,构造函数会按什么顺序(以及为什么按这个顺序)被调用。 如果我实例化类 C,那么它不应该只检查类 C 是否有任何构造函数,如果有,它应该使用它吗?

为什么会输出->在 A 在 B 在 C?

难道只有当它在自己的类中找不到构造函数时,它才会在层次结构中上升吗?或者每次都会隐式调用超类的构造函数?

I have three classes:

public class A {
    public A(){
        System.out.println("in A");
    }
}


public class B extends A{

    public B(){
        System.out.println("in b");
    }
} 


public class C extends B{

    public C(){
        System.out.println("in C");
    }   
}

Now I am really not sure how the constructor calls work.
If I instantiate C c= new C();, in what order (and why that order) does the constructors get called.
If I instantiate the class C, then should not it just check if the class C has got any constructor or not and if it does, it shall use it?

Why does it output-> In A In B In C?

Doesn't it go up in the hierarchy only when it doesn't find the constructor in it's own class? Or the constructors of the super class are called implicitly every time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

以可爱出名 2024-12-12 16:11:57

默认情况下,从基类构造函数调用超级构造函数。

唯一应该使用 super 调用超类构造函数的情况是当您需要显式地将参数传递给超类构造函数本身时。

所以答案是肯定的,都是叫的。顺序是从层次结构中的最高类向下到基类,因此:A、B、C。

Super constructor are called by default from the base class constructor.

The only case in which you should call a super class constructor with super, is when you need to explicitly pass a parameter to the super class constructor itself.

So the answer is yes, they are all called. The order is from the most up class in the hierarchy down to the base class, so : A, B, C.

东风软 2024-12-12 16:11:57

默认构造函数总是被调用,并且是自上而下调用的——从最顶层的超类到最低的基类。

请注意,只有参数化构造函数才需要 super(...) 调用。在您的情况下,您没有任何,因此默认值会自动调用。

Default constructors are always invoked, and are invoked top-down - from the topmost super-class to the lowest base-class.

Note that the super(...) call is necessary only for parameterized constructors. In your case you don't have any, so the default gets called automatically.

能怎样 2024-12-12 16:11:57

当调用构造函数时,它首先调用 super(),因此虽然堆栈跟踪将显示:C->B->A,但实际上 A 将首先被调用,C 将最后被调用,因此打印将显示:

in A
in B
in C

When a constructor is invoked, it first calls super(), so though the stack trace will show: C->B->A, actually A will be invoked first and C will be invoked last, thus the printing will show:

in A
in B
in C
如此安好 2024-12-12 16:11:57

super() 是隐式的,如果没有声明构造函数,则作为默认构造函数。

构造函数向上运行:

C calls B, B calls A, A calls to Object that does nothing and return to A, then outputs and return the flow to B, that outputs and return to C that outputs.

super() is implicit, as the default constructors if no constructors declared.

The constructors run upwards:

C calls B, B calls A, A calls to Object that does nothing and return to A, then outputs and return the flow to B, that outputs and return to C that outputs.
神经暖 2024-12-12 16:11:57

默认情况下会调用超级构造函数。它在派生类的构造函数中的任何代码行之前执行。因此,如果您调用 new C(),A 的构造函数将运行,然后是 B 的构造函数,然后是 C 中的任何内容。

Super constructor is called by default. It is executed prior to any lines of code in the derived class's constructor. So if you call new C(), A's constructor is run, then B's, then anything in C's.

榆西 2024-12-12 16:11:57

是的,当您未定义任何构造函数时,它会隐式创建默认构造函数并调用它。现在,当你扩展任何类时,就像类 C 一样,它会使用它的构造函数,但它没有找到显式调用超类的方法,因此编译器隐式调用超类构造函数。这就是为什么当我们调用超类构造函数时,该语句总是首先像这样,

public C(){
   super();
}

如果您在调用超类构造函数之前编写任何语句,则会出现错误

Yes when you not define any constructor it will implicitly create the default construct and call it. Now when you extends any class then like class C it go with its constructor but there it didn't find to call the super class explicitly so the compiler implicitly call the super class constructor. that why when we call the super class constructor that statement always first like this way

public C(){
   super();
}

if you write any statement before calling the super class constructor you getting error

墨小墨 2024-12-12 16:11:57

看一下Java语言规范

“如果构造函数体不以显式构造函数调用开始,并且声明的构造函数不是原始类 Object 的一部分,则编译器隐式假定构造函数体以超类构造函数调用“super();”开始。 ,调用其直接超类的构造函数,不带参数。”

http://java.sun.com/docs /books/jls/second_edition/html/classes.doc.html#41652

Take a look at the Java language specification

"If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments."

http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#41652

我三岁 2024-12-12 16:11:57

A 类是 B 的超类,B 类是 C 的超类。这意味着 A 位于其中的顶部位置,其中 B 位于 C 的顶部,紧邻 A,C 位于 B 的底部。所以 A 类首先执行,然后执行 B,然后执行 C。这就像 Java 中超类的优先级一样。

Class A is the super class for B and class B is super class of C. That means A is at the top position among them, where B is in top for C, next to A and C is sitting bottom of B. So class A execute first and then B and then C. It's like some priority in Java for super classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文