【问题标题】:No enclosing instance of type Person is accessible无法访问 Person 类型的封闭实例
【发布时间】:2013-04-10 02:14:18
【问题描述】:

我想知道是否有人能告诉我为什么我在第 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();
    }

}

【问题讨论】:

标签: java


【解决方案1】:

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

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

创建内部类static 将允许它工作。静态内部类,即嵌套类,不需要外部类的实例,此处为Person。

【讨论】:

  • 实际上遵循某种逻辑继承也可以解决问题,但也许这要求太多;)
猜你喜欢
  • 2011-12-15
  • 2016-11-20
  • 2013-04-11
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2013-02-17
相关资源
最近更新 更多