简体   繁体   English

如果超类没有一个默认构造函数,为什么此Java代码中的子类具有默认构造函数?

[英]Why does the subclass in this Java code have a default constructor if the super-class does not have one?

I have looked around for the answer to this question, but so far I haven't understood this. 我到处寻找这个问题的答案,但到目前为止我还不了解。 I have 3 Java classes, BinaryTree and HuffmanTree (which extends BinaryTree) and HuffmanData (used in HuffmanTree, irrelevant to this question) 我有3个Java类,BinaryTree和HuffmanTree(扩展了BinaryTree)和HuffmanData(用于HuffmanTree,与该问题无关)

HuffmanTree explicitly calls super() in its constructors. HuffmanTree在其构造函数中显式调用super()。 What I am unable to understand is why does it include a call to the default constructor? 我无法理解的是为什么它包括对默认构造函数的调用? The superclass BinaryTree does not have a default constructor at all, so according to my understanding the code should throw an Exception, but it doesn't. 超类BinaryTree根本没有默认的构造函数,因此根据我的理解,代码应引发Exception,但没有。

Here is the code for BinaryTree : 这是BinaryTree的代码:

public class BinaryTree {
    private Comparable data;
    private BinaryTree left;
    private BinaryTree right;
    public final static BinaryTree NIL = new BinaryTree(null, null, null);

    // Constructor 1
    public BinaryTree (Comparable data, BinaryTree left, BinaryTree right) {
        this.data = data;
        this.left = left;
        this.right = right;
    }

    // Constructor 2
    public BinaryTree (Comparable data) {
        this(data, BinaryTree.NIL, BinaryTree.NIL);
    }
    .
    .
    . // other methods
}

And here is the code for HuffmanTree : 这是HuffmanTree的代码:

public final class HuffmanTree extends BinaryTree implements Comparable {
    private String[] table;
    private static final int NUM_CHARS = 256;
    private static final HuffmanTree NIL = new HuffmanTree();
              // Calls default constructor in this class, but how does it work?

    // Constructor 1 --- what does this do??
    private HuffmanTree() {}

    // Constructor 2
    private HuffmanTree (char c, int f) {
        super(new HuffmanData(c,f), NIL, NIL);
    }

    // Constructor 3 
    private HuffmanTree (HuffmanTree left, HuffmanTree right) {
        super(new HuffmanData( (char)0, left.frequency()+right.frequency()),
                                                 left, right);
    }
    .
    .
    . // other methods
}

Apologies if the question is unclear. 如果问题不清楚,我们深表歉意。 Please let me know how I can explain it better. 请让我知道如何更好地解释它。

This my friend would produce a compiler error no suitable constructor found for BinaryTree() . 这个我的朋友会产生一个编译器错误, no suitable constructor found for BinaryTree()

A default no-arg constructor would be created for a Class only if you don't provide any constructor for the class. 仅当您不为该类提供任何构造函数时,才会为该类创建默认的无参数构造函数。 If you provide a constructor to the class, then you have to create a no-arg constructor on your own. 如果为该类提供了构造函数,则必须自己创建一个无参数的构造函数。

JLS states that JLS指出

It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor (§6.6) that takes no arguments and has no throws clause. 如果隐式声明了默认构造函数,但超类没有可访问的构造函数(第6.6节),该构造函数不带参数且不包含throws子句,则这是编译时错误。

The private no-args constructor does nothing useful except cause a compilation error. 私有的无参数构造函数除了会导致编译错误外,无济于事。 Looks like somebody's attempt to make it unusable by making it private, but the presence of the other constructor already prevents the compiler from generating a default constructor. 看起来有人试图通过将其设为私有来使其无法使用,但是其他构造函数的存在已经阻止了编译器生成默认构造函数。

I don't know why you think an exception will be thrown, unless you're confusing exceptions and compile errors. 我不知道为什么您会认为会引发异常,除非您混淆了异常并编译了错误。 Compile errors are not exceptions, and they are printed, not thrown. 编译错误不是例外,它们是打印的而不是抛出的。

In Java, default constructor is automatically created by JVM whether you write it or not. 在Java中,无论是否编写JVM,都会自动创建默认构造函数。 So in your code for class of BinaryTree default constructor is automatically created and when object of HuffmanTree calls default constructor it calls default constructor of BinaryTree. 因此,在您的BinaryTree类代码中,会自动创建默认构造函数,当HuffmanTree对象调用默认构造函数时,它将调用BinaryTree的默认构造函数。

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

相关问题 为什么子类必须在超类中调用no args构造函数? - why does the subclass have to invoke the no args constructor in the super class? java抽象类,构造器未在超类中调用,为什么? - java abstract class, Constructor not called in super-class, Why? 为什么添加一个超级类会破坏我的Spring bean? - Why does adding a super-class break my Spring beans? 子类是否需要构造函数? - Does a subclass NEED to have a constructor? 在 Java 中,您可以将子类的对象存储为超类类型,为什么要这样做? - In java you're able to store objects of a subclass as a super-class type, why would you do this? 为什么Java StringBuilder有CharSequence的构造函数和String的另一个构造函数? - Why does Java StringBuilder have a constructor for CharSequence and another one for String? 为什么在访问字段之前必须调用超级构造函数? - Why does the super constructor have to be called before fields can be accessed? 对于Serializable超类,如果我们对子类进行序列化,为什么调用super构造函数 - For a Serializable super class, if we serialize the subclass, why does the super constructor is invoked Java:Object类有构造函数吗? - Java: does Object class have a constructor? 为什么Calendar类没有公共构造函数? - Why does the Calendar class not have a public constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM