[英]How to initialize a new field of an inner class with constructor?
If I have this class and I want to initialize a new field of type Element
, how I can do that 如果我有这个类,我想初始化一个
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;
}
}
}
when I try to do this it gave me an error 当我尝试这样做时,它给了我一个错误
public class PriorityTest{
public static void main(String[]args){
MyLinkedList.Element e1 = new MyLinkedList.Element("any", 4, null);
}
}
Make the inner calss static 让内心保持静止
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);
}
}
Try this 尝试这个
MyLinkedList.Element e1 = new MyLinkedList().new Element("any", 4, null);
your inner class is not static
so you need to create an object of outer class first. 你的内部类不是
static
所以你需要先创建一个外部类的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.