java中定义动态内部类,静态的主函数不能直接实例化该类解决方案可以考虑将内部类定义成静态的。
示例:
public class Main {
/*
*此处的节点定义成静态
*/
static class Node
{
int data;
int weight;
public Node(int data,int weight)
{
this.data = data;
this.weight = weight;
}
}
public static void main(String[] args) {
//在主函数中实例化有效果
Node n2 = new Node(0,0);
}
}
如果内部类不适用静态,会出现错误
No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main).