I created a class of objects called questions
, in each question there is a Question and several Answers. I would like to create a second class of objects called games
and have several questions in each game. I would then like to have several games (with several questions) stored locally in a sharedObject
?
我创建了一类叫做问题的对象,在每个问题中都有一个问题和几个答案。我想创建第二类称为游戏的对象,并在每个游戏中有几个问题。那么我想在sharedObject中本地存储几个游戏(有几个问题)?
Is this considered "nesting" an object within another object?
这被认为是“嵌套”另一个对象中的对象吗?
1 个解决方案
#1
Here's some generic code for a Game class. It contains an array of questions. This basically means it contains as many Question objects as you want that are accessible through the addQuestion()
and getQuestion()
functions.
这是Game类的一些通用代码。它包含一系列问题。这基本上意味着它包含可以通过addQuestion()和getQuestion()函数访问的任意数量的Question对象。
public class Game {
private var questions:Array = [];
public function Game() {
}
public function addQuestion(q:Question):void {
questions.push(question);
}
public function getQuestion(i:uint):Question {
return questions[i];
}
}
Likewise, here is the class for a question. It contains a prompt, and an array of answers. Each answer is basically just a String, so there's no real need for an Answer class. If, however, you wanted to expand on this implementation so that each Answer also has other attributes, such as a timestamp, you could make a class for it.
同样,这是一个问题的类。它包含一个提示和一系列答案。每个答案基本上只是一个字符串,因此不需要一个Answer类。但是,如果您希望扩展此实现,以便每个Answer还具有其他属性,例如时间戳,则可以为其创建类。
public class Question {
private var prompt:String;
private var answers:Array = [];
public function Question(prompt:String) {
this.prompt = prompt;
}
public function getPrompt():String {
return prompt;
}
public function addAnswer(answer:String):void {
answers.push(answer);
}
public function getAnswer(i:uint):String {
return answers[i];
}
}
So to access the second answer of the first question in a game, you would write 'myGame.getQuestion(0).getAnswer(1)`.
因此,要访问游戏中第一个问题的第二个答案,您可以编写'myGame.getQuestion(0).getAnswer(1)`。
Hope this helped. Let me know if you have any more questions.
希望这有帮助。如果您还有其他问题,请与我们联系。
#1
Here's some generic code for a Game class. It contains an array of questions. This basically means it contains as many Question objects as you want that are accessible through the addQuestion()
and getQuestion()
functions.
这是Game类的一些通用代码。它包含一系列问题。这基本上意味着它包含可以通过addQuestion()和getQuestion()函数访问的任意数量的Question对象。
public class Game {
private var questions:Array = [];
public function Game() {
}
public function addQuestion(q:Question):void {
questions.push(question);
}
public function getQuestion(i:uint):Question {
return questions[i];
}
}
Likewise, here is the class for a question. It contains a prompt, and an array of answers. Each answer is basically just a String, so there's no real need for an Answer class. If, however, you wanted to expand on this implementation so that each Answer also has other attributes, such as a timestamp, you could make a class for it.
同样,这是一个问题的类。它包含一个提示和一系列答案。每个答案基本上只是一个字符串,因此不需要一个Answer类。但是,如果您希望扩展此实现,以便每个Answer还具有其他属性,例如时间戳,则可以为其创建类。
public class Question {
private var prompt:String;
private var answers:Array = [];
public function Question(prompt:String) {
this.prompt = prompt;
}
public function getPrompt():String {
return prompt;
}
public function addAnswer(answer:String):void {
answers.push(answer);
}
public function getAnswer(i:uint):String {
return answers[i];
}
}
So to access the second answer of the first question in a game, you would write 'myGame.getQuestion(0).getAnswer(1)`.
因此,要访问游戏中第一个问题的第二个答案,您可以编写'myGame.getQuestion(0).getAnswer(1)`。
Hope this helped. Let me know if you have any more questions.
希望这有帮助。如果您还有其他问题,请与我们联系。