我的类应该是什么类型才能访问类及其扩展的类?
您有
public class Question
和
public class MultipleChoice extends Question
主要
public class SurveyQuestions
SurveyQuestions 有
private static List<Question> q = new ArrayList<Question>();
您不断添加问题以及问题组成的所有内容到列表中。
完成后,您想要迭代列表
for (Question q : SurveyQuestions.getInstance().getListOfQuestions())
Question:“q”应该是什么类型,以便它可以访问 Question 和 MultipleChoice?
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 the 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果“访问”的意思是“访问...的方法”,则不能将其作为 MultipleChoice,因为有些问题可能并非如此。所以它必须是
Question
,然后你可以通过if(q instanceof 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 beQuestion
, and then you can check if it is a multiple-choice byif(q instanceof MultipleChoice)
您应该将其设为
问题
。您不能将
MultipleChoice
作为一个普通的问题
,不是一个MultipleChoice
...而是一个MultipleChoice
是一个问题
。所以Question
可以代表这两种类型。You should make it
Question
.You can't make it
MultipleChoice
as a plainQuestion
is not aMultipleChoice
... but aMultipleChoice
is aQuestion
. SoQuestion
can represent both types.