/**
 * 红色标注错误地方
 */public class Persont {
private  String name;
private int age;
public void setName(String name){
this.name=name;

}
public void setAge(int age)
{
this.age=age;

}
   public String getName(){
  return name;
  
   }
   public int getAge(){
   return age;
   
   }
   
  class Student extends Persont{
private String school;

public String getSchool(){
return school;

}

public void setSchool(String school){
this.school=school;


}

}
/**
 * @param args
 */
public static void main(String[] args)
{
Student student = new Student(); student.setName("weijin");

student.setAge(18);


student.setSchool("seles");

System.out.print(student.getName());
System.out.print(student.getAge());
System.out.print(student.getSchool());


// TODO Auto-generated method stub }}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
No enclosing instance of type Persont is accessible. Must qualify the allocation with an enclosing instance of type Persont (e.g. x.new A() where x is an instance of Persont). at Persont.main(Persont.java:43)

解决方案 »

  1.   

    在静态方法main中直接使用动态内部类Student当然不能通过编译了。
      

  2.   

    static class Student extends Persont 由于main是静态方法所以需要添加static
      

  3.   

    你贴错了,应该贴 Student.java
      

  4.   


    class Student extends Persont{
    private String school; public String getSchool(){
    return school; } public void setSchool(String school){
    this.school=school;
    } }
    放到代码的最外边}
      

  5.   

    应该Student student = new Persont().new Student();
      

  6.   

    或者 Persont.Student student = new Persont().new Student();
      

  7.   

    编译的java文件应该是Student.java,源代码没什么问题 ,就是在编译的Student.java文件的时候,把你上面源代码中public class Persont中的关键字public去掉,要不编译器会报错,主要是java文件允许有多个class,但是只能有一个用public修饰。因为java的编译机制是从这个public开始的。如果,你加的这个里面方法不是public static void main(),编译机制就会出错了。而且,你加的这个public的class名必须是你java文件的文件名,否则也无法编译。