简体   繁体   English

隐式调用超类中的无参数构造函数

[英]Calling no-argument constructor in superclass implicitly

This is taken from : http://docs.oracle.com/javase/tutorial/java/IandI/super.html 摘自: http : //docs.oracle.com/javase/tutorial/java/IandI/super.html

Note: If a constructor does not explicitly invoke a superclass constructor, the 注意:如果构造函数未明确调用超类构造函数,则

Java compiler automatically inserts a call to the no-argument constructor of Java编译器会自动插入对的无参数构造函数的调用

the superclass. 超类。 If the super class does not have a no-argument constructor, 如果超类没有无参数的构造函数,

you will get a compile-time error. 您将得到一个编译时错误。 Object does have such a constructor, so 对象确实具有这样的构造函数,所以

if Object is the only superclass, there is no problem. 如果Object是唯一的超类,则没有问题。

I made this small example to test that : 我做了这个小例子来测试:

class P as superclass (its empty class): P类为超类(其空类):

package org.standro.com.pk1;


public class P {
}

and class N in different package , class N extends class P without explicilty calling super() : 和类N在不同的程序包中,类N扩展了类P,而没有显式调用super():

import org.standro.com.pk1.P;


public class N extends P {

    public N() {
        //imlicitly super() is called here .. that means the constructor of P 

    }

    public static void main(String[] arg){
    N n=new N();

    }
}

and no compile error.. Im using JDK1.7 whats wrong with this example and why I don't get error ? 而且没有编译错误。.我使用JDK1.7,此示例有什么问题,为什么我没有收到错误?

I think the bold sentence above should be : 我认为上述粗体字应为:

If the super class does not have a any constructor,.... 如果超类没有任何构造函数,则...

because if there is at least one constructor .. compiler will get error.. 因为如果至少有一个构造函数,编译器将报错。

or please if anyone has explanation .. 或者如果有人有解释..

thanks 谢谢

Since the class P extends object, and has no constructor, it's constructor defaults to the no argument constructor which implicitly invokes the constructor in Object. 由于类P扩展了对象,并且没有构造函数,因此它的构造函数默认为no参数构造函数,该构造函数隐式调用Object中的构造函数。

To cause an error, you need to add a parameter to P's constructor like this: 要导致错误,您需要像这样向P的构造函数添加一个参数:

public class P {

    public P(int a) {
    }
}

Now N tried to implicitly call super() which looks for a no-args constructor in P. However, since we added a constructor in P, the default no-args constructor can no longer be used. 现在N试图隐式调用在P中寻找无参数构造函数的super()。但是,由于我们在P中添加了构造函数,因此无法再使用默认的无参数构造函数。

Claification: Claification:

If there is no constructor, default no-arg one is added. 如果没有构造函数,则默认添加no-arg。 If there is a constructor of any kind, the default no-arg constructor is not added 如果存在任何类型的构造函数,则不会添加默认的no-arg构造函数

As per your explanation only, if subclass is not calling super class constructor then automatically compiler inserts an internal call to super class's no arg constrcutor. 仅按照您的解释,如果子类未调用超类构造函数,则编译器会自动将内部调用插入到超类的no arg构造函数中。

so in class N ,you have N() constructor and its internally calling P class no arg constructor. 因此在类N中,您具有N()构造函数及其内部调用的P类,而没有arg构造函数。

and P internally calls Object class no arg constructor. 和P内部调用Object类的arg构造函数。

so your code will compile successfully. 因此您的代码将成功编译。

if there is no corresponding constructor in super class then only it complains. 如果超类中没有相应的构造函数,则只会抱怨。

so try to put some parameterized constrcutor in base class P(like p(int x){ }) and run your program. 因此,请尝试将一些参数化的构造函数放在基类P(like p(int x){ })并运行程序。 you will surely get error because of no arg costrcutor is not available in super class. 由于没有arg costrcutor在超类中不可用,您肯定会得到错误。

Note : no arg constrcutor is inserted only if there is no constrcutor at all in the class. 注意:仅当类中根本没有构造函数时,才插入arg构造函数。

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

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