简体   繁体   English

Java:将构造函数继承到子类

[英]Java: Inheriting constructor to the subclass

As i knew, constructors,Instance Initialization block are NOT inherited to the subclass, but below code inherits the super-class constructor, why it is calling? 如我所知,构造函数,实例初始化块不会继承到子类,但是下面的代码继承了超类构造函数,为什么要调用它?

Expected output is : from cons 2 预期输出为 :来自缺点2

but its showing output like : --IIB-- from cons 1 from cons 2 但其显示的输出如下 :--IIB--来自缺点1来自缺点2

WHY? this output , *As i know "sub class should not inherit Constructor & IIB block"* 

Please help me to make clear this concept. 请帮助我阐明这个概念。

public class X
{ 
    {
        System.out.println("--IIB--");
    }       

    X()
    {
        System.out.println("from cons 1");
    }
}   

class Y extends X
{
     Y()
     {
         System.out.print("from cons 2");
     }
}    


class Z 
{
    public static void main(String args[])
    {
        Y ob=new Y(); 
    }
} 

This is happening because this: 发生这种情况是因为:

Y()
 {
     System.out.print("from cons 2");
 }

actually becomes 实际上变成

Y()
 {
     super();  //Call to X's constructor made here even if you don't call it
     System.out.print("from cons 2");
 }

The reason for this is that every Y is also an instance of X . 这是因为每个Y也是X的实例。 It's necessary to call that parent constructor first, before any of the child constructor executes, to guaranteee that the parent attributes are ready to go. 必须在执行任何子构造函数之前先调用该父构造函数,以确保可以使用父属性。

Edit : here's the example to show that "super class constructors are not inheriated by the subclass": 编辑 :以下示例显示“子类未继承超类构造函数”:

class A {
    A(int intForA) { 
        //Do stuff...
    }
}

class B extends A {
    B() {
        this(3);  //Won't compile! A's constructor isn't inherited by B
    }
}

Instead, we do this: 相反,我们这样做:

class B extends A {
    B() {
        super(3);  //This compiles; we're calling A's constructor from B
    }
}

"The Java compiler copies initializer blocks into every constructor." “ Java编译器将初始化程序块复制到每个构造函数中。” - http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html -http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

"If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass." “如果构造函数没有显式调用超类构造函数,则Java编译器会自动将调用插入到超类的无参数构造函数中。” - http://docs.oracle.com/javase/tutorial/java/IandI/super.html -http://docs.oracle.com/javase/tutorial/java/IandI/super.html

When you call the sub class constructor, it internally calls the super class constructor using the super() (Note section in the linked page at the last) and in your case, the super class for Y is X . 当您调用子类构造函数时,它会在内部使用super()调用超类构造函数super()最后链接页面的“注意”部分),在您的情况下, Y的超类为X Also, the instance blocks are copied in the constructor and thus are executed when any of the constructors of that class are called. 同样,实例块被复制到构造函数中,并在调用该类的任何构造函数时执行。

From the init block docs 来自init块文档

Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword: 实例变量的初始化程序块看起来像静态初始化程序块,但没有static关键字:

{
    // whatever code is needed for initialization goes here
}

The Java compiler copies initializer blocks into every constructor. Java编译器将初始化程序块复制到每个构造函数中。

Thus the output in the below order. 因此按以下顺序输出。

--IIB-- - From the instance block which gets placed inside the constructor of X . --IIB-- ---从实例块放入X的构造函数中。

from cons 1 - when super() is called inside Y() from cons 1当在Y()内部调用super()

from cons 2 - the SOP in Y() from cons 2 - Y()的SOP

in you code Y extends X so ideally when you create Y class object it also create X class object. 在您的代码中Y extends X因此理想情况下,当您创建Y类对象时,它也会创建X类对象。 So its block gets execute first then constructor of X and then constructor of Y . 因此,其块首先执行,然后执行X构造函数,然后执行Y构造函数。

Hence you are getting output like that. 因此,您将获得类似的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM