Java 构造函数不能应用于给定的类型?

Java 构造函数不能应用于给定的类型?,java,Java,我有以下Java代码: public class WeirdList { /** The empty sequence of integers. */ /*ERROR LINE */ public static final WeirdList EMPTY = new WeirdList.EmptyList(); /** A new WeirdList whose head is HEAD and tail is TAIL. */ public WeirdList

我有以下Java代码:

public class WeirdList {
    /** The empty sequence of integers. */
    /*ERROR LINE */ public static final WeirdList EMPTY = new WeirdList.EmptyList();

    /** A new WeirdList whose head is HEAD and tail is TAIL. */
    public WeirdList(int head, WeirdList tail) {
        headActual = head;
        tailActual = tail;
    }
    /** Returns the number of elements in the sequence that
     *  starts with THIS. */
    public int length() {
        return 1 + this.tailActual.length();
    }

    /** Apply FUNC.apply to every element of THIS WeirdList in
     *  sequence, and return a WeirdList of the resulting values. */
    public WeirdList map(IntUnaryFunction func) {
        return new WeirdList(func.apply(this.headActual), this.tailActual.map(func));
    }

    /** Print the contents of THIS WeirdList on the standard output
     *  (on one line, each followed by a blank).  Does not print
     *  an end-of-line. */
    public void print() {
        System.out.println(this.headActual);
        this.tailActual.print();
    }

    private int headActual;
    private WeirdList tailActual;
    private static class EmptyList extends WeirdList {

        public int length() {
            return 0;
        }
        public EmptyList map(IntUnaryFunction func) {
            return new EmptyList();
        }
        public void print() {
            return;
        }
}

我不断得到错误:“构造函数不能应用于给定的类型”。。。这是否意味着超类的子类在构造函数中必须具有与超类相同数量的参数?我已经撞了一个小时的墙。

子类不必有任何构造函数“与超类在构造函数中的参数数相同”,但它必须从自己的构造函数调用其超类的一些构造函数

如果超类有一个无参数构造函数,默认情况下,如果省略了对超类构造函数的显式调用,或者如果子类根本没有显式构造函数(如您的情况),则会调用它,但由于您的超类没有无参数构造函数,编译失败

您可以将类似的内容添加到
空列表中

private EmptyList() {
    super(0, null);
}

还有一个更好的主意,就是要有一个抽象的超类,这两个类都从中继承,但这是一个选择。

您需要向WeirdList显式添加一个无参数构造函数,因为EmptyList有一个默认的无参数构造函数,但它在可以调用的超类中没有构造函数。

当您调用任何方法时,调用和callie必须匹配参数和返回类型。构造函数的特殊情况是,如果您不创建一个构造函数,编译器将插入一个空构造函数void wird(){}您可以重载一个类并拥有许多构造函数,将执行的构造函数是具有相同签名ie类型的构造函数。您尝试的是在LinkedList、Vector、,列出java类。

您的
EmptyList
internal
class
缺少构造函数,这是调用父类构造函数所必需的。因为没有默认的父构造函数,所以内部类必须手动调用父构造函数。应该注意的是,
WeirdList
的构造函数很奇怪,因为
head
tail
的类型不同,它需要一个
tail
,即使您的
EmptyList
显然没有。我建议使用
接口
,它
清空列表
可以
实现
,然后使用更感性的
古怪列表
构造函数。