No enclosing instance of type test5 is accessible. Must qualify the allocation with an enclosing instance of type test5 (e.g. x.new A() where x is an instance of test5).
在一个类里面的main函数,经常会写一些测试代码。
在调用这个类里面的内部类的时候,出现不能初始化内部类的对象实例。
发现两种解决方法:
1、把内部类换成静态类
2、直接把在main函数初始化的内部类,挪到类里面的一个方法里面即可。
原先出问题的代码
public class W extends Wifi<String>
{
@Override
public String getNet()
{
// TODO Auto-generated method stub
return "WWWW";
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
W ww = new W();
ww.net();
ww.ondo();
System.out.println(ww.getNet());
}
修改之后:
public class W extends Wifi<String>
{
@Override
public String getNet()
{
// TODO Auto-generated method stub
return "WWWW";
}
}
public void testW()
{
W ww = new W();
ww.net();
ww.ondo();
System.out.println(ww.getNet());
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
test5 test5 = new test5();
test5.testW();
}