you have
public class Question
and
public class MultipleChoice extends Question
and
public class SurveyQuestions
where SurveyQuestions has
private static List<Question> q = new ArrayList<Question>();
In main you keep adding questions and all the things questions are composed of to 开发者_如何学JAVAthe list.
Once done, you want to iterate over the list
for (Question q : SurveyQuestions.getInstance().getListOfQuestions())
Question: What type should "q" be so that it has access to both Question and MultipleChoice?
If by "access to" you mean "access to the methods of"- you can't have it as MultipleChoice
because some questions might not be such. So it has to be Question
, and then you can check if it is a multiple-choice by if(q instanceof MultipleChoice)
You should make it Question
.
You can't make it MultipleChoice
as a plain Question
is not a MultipleChoice
... but a MultipleChoice
is a Question
. So Question
can represent both types.
精彩评论