<< Chapter < Page Chapter >> Page >

Populated with six component objects

It is also worth noting that the array elements are populated with references to component objects for QuizB when the array is created in Listing 3. The new component objects aren't instantiated until later in QuizA .

Implicit setter methods for QuizA

Listing 4 shows five implicit setter methods for the class named QuizA .

Implicit setter methods for quiza.

public function set question(textIn:String):void{ theQuestion.text = textIn;}//end implicit setterpublic function set answer(answerIn:String):void{ theAnswer = answerIn;}//end implicit setterpublic function set choice0(choice:String):void{ choice00.label=choice;}//end implicit setterpublic function set choice1(choice:String):void{ choice01.label=choice;}//end implicit setterpublic function set choice2(choice:String):void{ choice02.label=choice;}//end implicit setter

Setter methods are called by mxml code

Briefly, these methods are called by the code in Listing 1 when values are assigned to the following five mxml attributes:

  • question
  • choice0
  • choice1
  • choice2
  • answer

See Defining Custom MXML Components here if you are interested in learning more about implicit setter methods at this point in time.

Implicit setter methods for QuizB

Listing 5 shows five implicit setter methods for QuizB that serve the same purpose as the five implicit setter methods for QuizA . Note the differences in the code that results from using individual variables toreference the components in QuizA and using an array to reference the components in QuizB .

Implicit setter methods for quizb.

public function set question(textIn:String):void{ components[0].text = textIn; }//end implicit setterpublic function set answer(answerIn:String):void{theAnswer = answerIn; }//end implicit setterpublic function set choice0(choice:String):void{components[1].label=choice;}//end implicit setterpublic function set choice1(choice:String):void{ components[2].label=choice; }//end implicit setterpublic function set choice2(choice:String):void{components[3].label=choice;}//end implicit setter

Expose the interface but hide the implementation

These five setter methods, along with the class constructors, constitute the entire user interface for each class. If you examine Listing 13 andListing 14, you will see that these five setter methods and the constructor are the only public members of either class. All other members of the classes are declared private .

Furthermore:

  • The names of the five methods are the same in both classes.
  • The names and types of the required parameters for the five methods are the same in both classes.
  • The five methods serve the same purpose in both classes.
  • The ultimate behavior of objects instantiated from the two classes is the same.

Therefore, the exposed user interface is the same for both classes but the hidden implementation is significantly different between the two classes.

Purpose of the setter methods

The purpose of the setter methods in both cases is to store mxml attribute values in the text property of the TextArea at the top of each question object in Figure 1 and to store mxml attribute values in the label property of each of the RadioButton objects in each question object in Figure 1. In addition, one of the setter methods stores anattribute value in the variable named theAnswer .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask