我的程序中有以下课程:
public abstract class Question {
private Topic topic;
private String text;
// methods
}
public class OpenQuestion extends Question {
// methods
}
public class MultipleChoiceQuestion extends Question {
private List<String> options = new ArrayList<String>();
private String correct;
// methods
}
此外,我还有另一个类测试:
Question question;
// if the question is open
if(question instanceof OpenQuestion) {
...
}
// if the question is multiple choice
if(question instanceof MultipleChoiceQuestion) {
...
}
我正在尝试寻找替代方案instanceof
,因为有人告诉我它违反了 OOP 原则。
还有其他更好的方法可以知道问题是开放式还是多项选择?