2013-04-19 3 views
1

Java 프로젝트에서 작업하고 있는데 클래스 중 하나에서 내부 중첩 클래스 (TypesOfQuestions)를 사용하고 내부 클래스의 질문을 HashMap에 추가하려고하면 내가 왜 그랬는지 모르겠다.Java 내부 클래스와 HashMaps

public abstract class TypesOfQuestions { 

Map<String, Double> scores = new TreeMap<String, Double>(); 
String text; 
double points; 

public TypesOfQuestions(String text, double points) { 

} 


public static class TrueFalse extends TypesOfQuestions { 
    public TrueFalse(String text, double points, boolean answer) { 
     super(text, points); 
    } 
}  

그리고 다른 클래스는 시험이다,

public class Exam { 
private String x; 
private Map<Integer, Questions> q; 

public HoldExam(String x) { 
    q = new HashMap<Integer, Questions>(); 
    this.x = x; 
} 

public void addTrueFalseQuestion(int questionNumber, String text, 
           double points, boolean answer){ 
    q.put(questionNumber, new TrueFalse (text, points, answer)); 

} 
} 

나는 여러 가지의 무리를 시도했습니다,하지만 난

No enclosing instance of type TypesOfQuestions is accessible. 
Must qualify the allocation with an enclosing instance of type TypesOfQuestions 
(e.g. x.new A() where x is an instance of TypesOfQuestions). 

을 가지고이 일에 대한 오류가 계속 Anndddd 나는 무엇을해야하는지 전혀 모른다!!

답변

3

TrueFalsestatic이어야합니다. 외부 인스턴스 인 TypesOfQuestions과 연결되어 있지 않아야합니다. 이고 TypesOfQuestions입니다.

+0

오, 안돼! 잠깐, 그래서 당신이 말한 것처럼 정적으로 바꿨고, 저장을 시도 할 때까지 오류를 수정했다가 오류가 다시 발생했습니다. 같은 오류. – CCC

+0

여기서'Questions' 클래스는 어디에 정의되어 있습니까? 우리는'Questions'에 대한 정의가 없습니다. –

+0

죄송합니다. 질문은 TypesOfQuestions이어야하며, 입력하지 않았습니다. 또한, 일식 그것을 받아 들일 것 다음 그것을 받아 들일 수 없다 ... 그것은 때때로 오류를주고, 다른 시간은 아니지만 ...하지만 내 코드를 변경하지 않을거야. – CCC

1

중첩 클래스를 정적으로 변경하여 외부 클래스에 대한 암시 적 참조가 필요하지 않도록합니다.

public static class TrueFalse extends TypesOfQuestions { ... } 

내부 클래스 정적 한정자없이 는 외부 클래스의 멤버에 액세스 할 수 있습니다. 이를 위해서는 외부 클래스에 대한 암시 적 참조가 있어야합니다.