2013-04-10 85 views
0

我想知道是否有人能告诉我为什么我上线175没有封闭类型的人的情况下被访问

Student george= new Student("George Flintstone", 21, "Politics", 3.1); 

跟它有访问类型的人没有外围实例得到一个错误。我已经浏览了其他类似的问题,但是我对Java不是很熟悉,所以我没有真正找到答案,至少我理解的很好。这是从我的介绍到java类的作业。我没有在常见问题解答中看到任何东西,我不能要求帮助做家庭作业,但是生病承认我没有仔细阅读,所以如果我打破了某种规则或其他方式,请让我知道。

此外,抱歉张贴这么多的代码,我不知道它的哪些部分可能是重要的。

Public class Person { 

    String name; 
    int age; 

    public Person(String n, int a) 
    { 
     name = n; 
     age = a; 
    } 

    public Person() 
    { 
     name = "Bob"; 
     age = 24; 
    } 

    public void set_Person(String n) 
    { 
     name = n; 
    } 

    public void set_Person(int a) 
    { 
     age = a; 
    } 

    public void set_Person(String n, int a) 
    { 
     name = n; 
     age = a; 
    } 

    public String get_Name() 
    { 
     return name; 
    } 

    public int get_Age() 
    { 
     return age; 
    } 

    public boolean equals(Object ob) 
    { 
     Person p = (Person)ob; 
     if(ob == null) 
      return false; 
     if(name.equals(p.name) && age == p.age) 
      return true; 
     else 
      return false; 
    } 

    public String toString() 
    { 
     String temp = name + " is " + age + " years old "; 
     return temp; 
    } 

class Student { 

    String major; 
    double gpa; 
    Person p = new Person(); 

    Student(String n, int a, String m, double g) 
    { 
     p.set_Person(n,a); 
     major = m; 
     gpa = g; 
    } 

    Student() 
    { 
     p.set_Person("Ben", 10); 
     major = "compsci"; 
     gpa = 3.5; 
    } 

    public void set_Student(String n, int a, String m, double g) 
    { 
     p.set_Person(n,a); 
     major = m; 
     gpa = g; 
    } 

    public void set_Student(String n) 
    { 
     p.set_Person(n); 
    } 

    public void set_Student(int a) 
    { 
     p.set_Person(a); 
    } 

    public String getStudentName() 
    { 
     String temp = p.get_Name(); 
     return temp; 
    } 

    public int getStudentAge() 
    { 
     return p.get_Age(); 
    } 


} 

class Family { 


    Person [] per_array; 
    int person_count = 0; 
    Person temp = new Person(); 

    Family(int members) 
    { 
     Person[] per_array = new Person[members]; 
    } 

    public void addPerson(Person p) 
    { 
     boolean check = false; 

     for(int count = 0; count < per_array.length; count++){ 
      if(per_array[count].equals(p)) 
      { 
       System.out.println("That person is already a member of this family"); 
       check = true; 
      } 
     } 
     if(check = false){ 
      per_array[person_count] = p; 
      person_count++; 
     } 
    } 

    public void addPerson(Student s) 
    { 
     temp.set_Person(s.getStudentName(), s.getStudentAge()); 
     boolean check = false; 

     for(int count = 0; count < per_array.length; count++){ 
      if(per_array[count].equals(temp)) 
      { 
       System.out.println("That person is already a member of this family"); 
       check = true; 
      } 
     } 

     if(check = false){ 
      per_array[person_count] = temp; 
      person_count++; 
     } 

    } 

    public void printOutFamily() 
    { 
     for(int count = 0; count < per_array.length; count++) 
     { 
      per_array[count].toString(); 
     } 
    } 
} 

    public static void main(String[] args) { 
     Person fred= new Person("Fred Flintstone", 50); 
     System.out.println("created " + fred); 

     Person wilma = new Person("Wilma Flintstone", 48); 
     Student george= new Student("George Flintstone", 21, "Politics", 3.1); 
     System.out.println("created " + george); 

     Student sue= new Student("Sue Flintstone", 24, "Nursing", 3.3); 
     Student anotherGeorge= new Student("George Flintstone", 21, "Math", 3.4); 
     Person yetAnotherGeorge= new Person("George Flintstone", 21); 

     Family f = new Family(10); 
     f.addPerson(fred); 
     f.addPerson(wilma); 
     f.addPerson(george); 
     f.addPerson(sue); 
     f.addPerson(anotherGeorge); 
     f.addPerson(yetAnotherGeorge); 

     anotherGeorge.set_Student("Georgie Flintstone"); 
     f.addPerson(anotherGeorge); 

     f.printOutFamily(); 
    } 

} 
+1

如果'Person'和'Student'在同一个文件中,很有可能你的大括号错位。 – dasblinkenlight 2013-04-10 02:17:56

+0

为什么'Student'不是'扩展''Person'呢? – MadProgrammer 2013-04-10 02:34:12

+0

可能重复[没有封闭的服务器类型的实例是可以访问的](http://stackoverflow.com/questions/7901941/no-enclosing-instance-of-type-server-is-accessible) – Raedwald 2016-03-02 22:32:21

回答

2

Student是其与的Person特定实例相关联的内部类,这意味着它是无效的调用

new Student("George Flintstone", 21, "Math", 3.4);* 

使内类static将允许它工作。静态内部类,也就是嵌套类,不需要外部类的实例,这里是Person

+0

其实遵循某种逻辑继承也会解决这个问题,但也许这是要求太多;) – MadProgrammer 2013-04-10 02:33:48

相关问题