When是在调用构造函数后创建的对象 [英] When is an Object created after a constructor is called

查看:179
本文介绍了When是在调用构造函数后创建的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码类。




     public class A
    {
        public A()
        {
            callCreation();
        }

        protected void callCreation()
        {
            System.out.println("A Created!!");
        }
    }




    public class B extends A
    {
        protected void callCreation()
        {
            System.out.println("B Created!!");
        }
    }


    public class C extends B
    {
        protected void callCreation()
        {
            System.out.println("C Created!!");
        }

        public static void main(String[] args)
        {
            A a = new A();
            A b = new B();
            A c = new C();
        }
    }

运行类C


A Created!!
B Created!!
C Created!!

打印输出

A Created!!

中的第一个输出行,调用A类的构造函数,它在调用A类构造函数中的callCreation()方法之前隐式调用超类的构造函数(java.lang.Object)。这也将是B和C类的情况。在这种情况下,当调用B的构造函数时,调用流应该通常是:B的构造函数 - > A的构造函数 - > java.lang.Object的构造函数 - >回到A的callCreation()方法来完成调用A的构造函数。如果是这样,如何打印重写的值,而不是打印超类的值?所以问题是何时是一个类的对象创建完全?换句话说,类的对象只有在构造函数完成调用/初始化它自身中的所有元素之后才被创建。

is printed because when the constructor of class A is called, it calls the super class's constructor (java.lang.Object) implicitly before calling the callCreation() method in the class A's constructor. And this will be the case for B and C classes too. In that case when the constructor of B is called the call flow should be typically : B's constructor -> A's Constructor -> java.lang.Object's Constructor -> come back to A's callCreation() method to finish calling A's constructor. If so how is the overridden value printed and not the super class's value is printed? So the question is 'when is an object of a class created exactly? to put it in other words, the object of a class should be created only after the constructor finishes calling/initializing all the elements within itself. If so how can a method be called from a child class and not from the parent class?

推荐答案

The callCreation methods in B and C override the method from A. So when the method is called in the constructor A the implementations in B and C will be run, even though the constructors of B and C have not been executed yet. This is possible because constructors don't actually create the object, rather they are called to initialize some moments after the JVM has created it.

一般来说,从这个原因调用可以从构造函数覆盖的方法是一个非常糟糕的主意。 B或C中的方法可以假设构造函数和对象初始化程序已经运行,并从字段中读取意外的值。例如,以下结束打印B Created !! null,因为该字段尚未分配其值。

In general it's a very bad idea to call methods that can be overridden from a constructor for this very reason. A method in B or C may assume that the constructor and object initializers have already been run, and read an unexpected value from a field. For example the following ends up printing "B Created!! null" because the field still has not be assigned its value.

public class B extends A
{
    final String msg = "Yes!";

    protected void callCreation()
    {
        System.out.println("B Created!! "+msg);
    }
}

这篇关于When是在调用构造函数后创建的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆