Dynamic Classes

 <  Day Day Up  >  

Throughout this chapter, you have learned about defining classes that have a fixed set of properties and methods. When you create instances of these classes, you expect the properties and methods to be available. The good news is that this is still true. All properties and methods in the class definition are in the instance.

However, what if you were using a class and you decided it needed a new property: For example, the Loan class could be used to calculate many different kinds of loans. To support different kinds of loans, you might have to add a fee specific to the loan type. Because these properties don't currently exist in the Loan class definition, you would have to go back to the Loan.as file, add the properties, and recompile it along with everything that uses the Loan class. You have to do this because an instance of a class can't create or access properties or methods that weren't originally declared or defined in the class. To do this, you would have to declare the class dynamic to add class members at runtime.

The dynamic class modifier lets you do just that. For example, Listing 2.13 adds the dynamic modifier to the Loan class.

Listing 2.13. Using the dynamic Class Modifier
 dynamic class Loan {   var principal:Number;   var rate:Number;   var term:Number; ... } 

Now, at runtime, instances of the Loan class can add and access properties and methods that were not defined in the original class. Listing 2.14 adds a loan origination fee to the loan instance and assigns it a value.

Listing 2.14. Adding an Instance Member to a Dynamic Class
 var loan:Loan = new Loan(); loan.originationFee = 150; // no compiler error because class is dynamic 

In the Loan class, you can customize the data returned to the user by adding a message indicating the type of loan for which he or she is searching. Because this data has no impact on the output of the loan calculation, there is no reason to bog down the class definition with properties that are specific to this particular user interface. You need to add loanType as a dynamic property of the loan instance by first declaring the class as dynamic and then adding a dynamic property to hold the loan type. In Figure 2.9, you can see the dynamic property added to loanCalc.fla when the loan payments are calculated.

Figure 2.9. Adding a dynamic property to the Loan class instance.


You then use this information to return a custom message. You could not do this in the earlier version of the Loan class. The code now has the dynamic modifier for the Loan.as file, as shown in Figure 2.10.

Figure 2.10. Outputting a dynamic class property by using loanType .


Type checking on dynamic classes is less strict than type checking on non-dynamic classes because members accessed inside the class definition and on class instances are not compared to those defined in the class. Class methods, however, are still type checked for return type and parameter types.

 <  Day Day Up  >  

Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net