我无法理解什么是“没有......的关闭实例”是什么意思?

时间:2015-08-15 12:40:26

标签: java

我是大一新生,我无法理解什么是“没有关闭.....的实例”是什么意思?

public static void main(String[] args) {
    Map<Pk_person, Person> map = new HashMap<Pk_person, Person>();
    Pk_person pk_person = new Pk_person(); //Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    //No enclosing instance of type Simple1 is accessible. 
    //Must qualify the allocation with an enclosing instance of type Simple1 (e.g. x.new A() where x is an instance of Simple1).
    pk_person.setPrefix("MR");
    pk_person.setNumber(22081);
    map.put(pk_person, new Person(pk_person, "马先生"));
    /*Pk_person pk_person2 = new Pk_person();
    pk_person2.setPrefix("MR");
    pk_person2.setNumber(22081);
    Person person2 = map.get(pk_person2);*/
    System.out.println(pk_person.getPrefix());      
}

1 个答案:

答案 0 :(得分:0)

Pk_person是类Simple1的内部类。就像没有Class实例的成员varibales一样,内部类需要包含外部类的实例。他们可以像其他成员varibales一样有任何访问修饰符。

你需要做这样的事情: -

new Simple1().new Pk_person();