开发者

What type should my class be to access a class and the class it extended?

开发者 https://www.devze.com 2023-01-12 20:52 出处:网络
you have public class Question and public class MultipleChoice extends Question and public class SurveyQuestions

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号