gpt4 book ai didi

java - 如何使用构造函数初始化内部类的新字段?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:15 24 4
gpt4 key购买 nike

如果我有这个类并且我想初始化一个 Element 类型的新字段,我该怎么做

public class MyLinkedList{

protected Element head, tail;

public final class Element{
Object data;
int priority;
Element next;

Element(Object obj, int priorit, Element element){
data = obj;
priority = priorit;
next = element;
}
}
}

当我尝试这样做时,它给了我一个错误

public class PriorityTest{
public static void main(String[]args){
MyLinkedList.Element e1 = new MyLinkedList.Element("any", 4, null);
}
}

最佳答案

使内部类静态化

public class MyLinkedList{

protected Element head, tail;

public static final class Element{
Object data;
int priority;
Element next;

Element(Object obj, int priorit, Element element){
data = obj;
priority = priorit;
next = element;
}
}

public static void main(String[]args){
MyLinkedList.Element e1 = new MyLinkedList.Element("any", 4, null);
}
}

关于java - 如何使用构造函数初始化内部类的新字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22729216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com