public class Test { /**
* @param args
*/
public class Music {
public void play(){
System.out.println("ok");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Music music=new Music();
在new的时候不能编译
//No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).
}}
为什么呢?各位大哥大姐帮忙解释一下
谢谢了
* @param args
*/
public class Music {
public void play(){
System.out.println("ok");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Music music=new Music();
在new的时候不能编译
//No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).
}}
为什么呢?各位大哥大姐帮忙解释一下
谢谢了
System.out.println("ok");
}应该是这里错了,看看构造函数吧,或许有用
因为main方法是static的,要么把Music声明为static,要么main方法的
Music music=new Music();改成Music music=new Test().new Music();
如:
public class Test {/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Music music=new Music();
music.play();
//No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).
}}
class Music {
public Music()
{}
public void play(){
System.out.println("ok");
}
}