Static, Public, and Private Properties and Methods


When working with class-based programming, you will most often use classes to define objects of which you will later create instances. Their properties and methods cannot be called directly, like a function, but must be accessed through an instance of the class.

However, you may want to use a method or property without creating an instance of its class. When working with your own custom classes, use the static keyword to declare a property or method that you want to use outside of a class's instance. ActionScript has several built-in static methods, such as Math.random and Math.cos. To use these methods, you don't create an instance of the Math object. You just call them directly. For example, the following code sets a variable to be equal to the cosine of 20:

var cosTwenty:Number = Math.cos(20);


By default, class methods and properties are public. Public properties and methods can be accessed from outside the class that defines it. For example, after an instance of Guest is created, you can call the setFavFood method for that instance:

guest1.setFavFood("pizza");


Private properties and methods, which are defined with the private keyword, can be accessed only from within the class. For example, you might use a private property when using Flash as the user interface to a database application. If you store the data for your guests in a database, each guest's record has a unique ID number. This isn't relevant information for the user interface, but it is important for passing information to the database.

private var guestID:Number;


Although you might not use the static and private keywords often, they will come in handy as you continue to develop applications with class-based ActionScript.



Special Edition Using Macromedia Studio 8
Special Edition Using Macromedia Studio 8
ISBN: 0789733854
EAN: 2147483647
Year: 2003
Pages: 337

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