User Guide

664 Chapter 2: ActionScript Language Reference
Book.prototype.addProperty("bookcount", Book.prototype.getQuantity,
Book.prototype.setQuantity);
Book.prototype.addProperty("bookname", Book.prototype.getTitle, null);
var myBook = new Book();
myBook.bookcount = 5;
trace("You ordered "+myBook.bookcount+" copies of "+myBook.bookname);
The following example shows how to use the implicit getter and setter functions available in
ActionScript 2.0. For more information, see “Implicit getter/setter methods” in Using ActionScript
in Flash.Rather than defining the
Book function and editing Book.prototype, you define the
Book class in an external file named Book.as. For more information, see “Creating and using
classes” in Using ActionScript in Flash. The following code must be in a separate external file
named Book.as that contains only this class definition and resides within the Flash applications
classpath:
class Book {
var books:Number;
function set bookcount(numBooks:Number):Void {
this.books = numBooks;
}
function get bookcount():Number {
return this.books;
}
function get bookname():String {
return "Catcher in the Rye";
}
}
The following code can then be placed in a FLA file and will function the same way as it does in
the previous examples:
var myBook:Book = new Book();
myBook.bookcount = 5;
trace("You ordered "+myBook.bookcount+" copies of "+myBook.bookname);