Вот еще один способ размышления о проблеме - есть ли разница между MathQuestion и WordQuestion? Для меня это звучит так, как будто они оба являются объектами Вопроса, и вы можете различать разные типы с помощью композиция .
Вы могли бы начать с определения класса Enum , в котором перечислены различные типы Вопросов, которые появляются в вашем Викторине (строго говоря, ActionScript 3 не имеет надлежащих Enums, но мы все же можем обеспечить безопасность типов с помощью следующего код.)
public class QuestionType {
public static const MATH : QuestionType = new QuestionType("math");
public static const WORLD : QuestionType = new QuestionType("world");
private var _name : String;
// Private constructor, do no instantiate new instances.
public function QuestionType(name : String) {
_name = name;
}
public function toString() : String {
return _name;
}
}
Затем вы можете передать одну из констант QuestionType в класс вопросов при его создании:
public class Question {
private var _message : String /* Which country is Paris the capital of? */
private var _answers : Vector.<Answer>; /* List of possible Answer objects */
private var _correctAnswer : Answer; /* The expected Answer */
private var _type : QuestionType; /* What type of question is this? */
public function Question(message : String,
answers : Vector.<Answer>,
correctAnswer : Answer,
type : QuestionType) {
_message = message;
_answers = answers.concat(); // defensive copy to avoid modification.
_correctAnswer = correctAnswer;
_type = type;
}
public function getType() : QuestionType {
return _type;
}
}
Наконец, клиент (код, который использует объект Question) может легко запросить тип вопроса:
public class QuizView extends Sprite {
public function displayQuestion(question : Question) : void {
// Change the background to reflect the QuestionType.
switch(question.type) {
case QuestionType.WORLD:
_backgroundClip.gotoAndStop("world_background");
break;
case QuestionType.MATH:
_backgroundClip.gotoAndStop("math_background");
break;
}
}
}