所以我在一个Java项目上工作,我的一个类使用内部嵌套类(TypesOfQuestions),当我尝试将内部类的问题添加到HashMap时,它不允许我这样做,而且我没有确定为什么。

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我不知道该怎么办!

最佳答案

TrueFalse应该是static,因为它与TypesOfQuestions的外部实例无关。它是一个TypesOfQuestions

09-25 21:04