当前位置: 代码迷 >> J2SE >> 新人 。有关JAVA 类和方法的有关问题
  详细解决方案

新人 。有关JAVA 类和方法的有关问题

热度:636   发布时间:2016-04-23 20:33:24.0
新人 求助。有关JAVA 类和方法的问题
package test;

public class DuiXiang {
public static void main(String[] args){
Lei a1 = new Lei();
a1.name = "莉莉";
a1.age = 20;
a1.setName("蓉蓉");
System.out.println("name:" + a1.name + "age:" + a1.age);
a1.Eat();
a1.Sleep();
}
class Lei{
String name;
int age;

public String getName(){
return name;
}
public void setName(String n){
name = n;
}
public void Sleep(){
System.out.println("睡觉");
}
public void Eat(){
System.out.println("吃饭");
}
}

}

为什么报错:No enclosing instance of type DuiXiang is accessible. Must qualify the allocation with an enclosing instance of type 
 DuiXiang (e.g. x.new A() where x is an instance of DuiXiang).
谢谢。
------解决方案--------------------
楼主你粗心了,最后一个大括号把前面的Lei变成了一个内部类
应该去掉最后一个大括号,在
   a1.Sleep();
    }
后加一个大括号
或则把第一句改为 
Lei a1 = new DuiXiang().new Lei();
  相关解决方案