这是我的主课:
import java.util.ArrayList;
public class MainClass {
public static void main(String[] args){
ArrayList<SecondClass.InnerClass> list=new ArrayList<SecondClass.InnerClass>();
list.add(new SecondClass.InnerClass()); //error here (read below)
}
}
这是第二堂课:
public class SecondClass {
public class InnerClass{
}
}
在 MainClass
的 list.add
处,我收到此错误:
No enclosing instance of type SecondClass is accessible. Must qualify the allocation with an enclosing instance of type SecondClass (e.g. x.new A() where x is an instance of SecondClass).
我需要使 InnerClass
非静态,因为 InnerClass
需要对非静态方法进行静态引用。如何在 ArrayList
中添加元素?
最佳答案
我认为你需要:
new SecondClass().new InnerClass()
关于java - 如何用内部类填充 Arraylist?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26303569/