首页 > 解决方案 > 为什么我不能使用嵌套的 IntList 创建对象?

问题描述

为什么我不能像在 Python 中那样使用嵌套的 IntList 创建实例?

我得到错误:找不到符号符号:方法 IntList(int,) 位置:类 IntList

class Link:

    empty = ()

    def __init__(self, first, rest=empty):
        assert rest is Link.empty or isinstance(rest, Link)
        self.first = first
        self.rest = rest

s = Link(3, Link(4, Link(5)))
public class IntList {
    public int first;
    public IntList rest;

    public IntList(int f, IntList r) {
        first = f;
        rest = r;
}

public static void main(String[] args) {
        IntList L = new IntList(15, IntList(10, null));

    }
}

标签: javainstantiation

解决方案


您需要添加new到您的第二个IntList实例:

IntList L = new IntList(15, new IntList(10, null));
                            ^^^

没有这个,它会试图找到一个名为IntList.


推荐阅读