Implementing Interfaces

 <  Day Day Up  >  

Using the concept of the interface to create a contract between certain documents and the Printable interface, we have a guarantee that if we have an instance of PaperDoc , it will have a method called print() . In addition, it will print correctly for that type of PaperDoc . Listing 6.5 shows a class called PrintQue that has a method print() . PrintQue is used by many different types of classes and regardless of the actual instance of the object, printing will take place based on the actual type of the class being printed.

Listing 6.5. Using the Printable Interface
 public class PrintQue {       public print (item:Printable){             item.print() } } 

Let's take a look at the LoanCalc example using an interface to provide printing functionality, as shown in Listing 6.6.

Listing 6.6. Using the Printable Interface with the LoanCalc Example
 interface Printable{       function print(); }class Loan implements Printable{ ...       function print( ) { // satisfy the contract by implementing print()             trace("Loan printing: "+this);             //using the interface here             //do some formatting and print       }}function calc(loan:Loan){ //function called by the button handler       //calculate the monthly payment       var monthlyPmtVal:Number =  loan.getMonthlyPayment();       //display monthly payment       monthlyPmt.text = monthlyPmtVal;       var totalPmtVal:Number =  loan.getTotalPayment();       //display total payments       totalPmt.text = totalPmtVal;       //print the loan  this.print(loan);  //call the print method passing in the loan instance } function print(  item:Printable  ){ // method argument is an interface instance trace ("in loancalc.print"); item.print(); } 
 <  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 © 2008-2017.
If you may any questions please contact us: