February 28, 2009
ActionScript 3.0 Defining Classes and Objects
Up until now I have been working with different objects. While they have so far been in their simplified, and implicit form, AS3 is a object oriented language with some basic objects like strings, numbers, integers, unsigned integers, and array’s which can be created implicitly, simply by its given value.
As I have mentioned before in my Objects and Punctuation Post. Objects in ActionScript3 are defined by classes, or blueprints if you will. These classes are kept in ActionScript files, each class inside its own package with a defined namespace.
A package in ActionScript is a collection of classes. Each class is wrapped with a package wraper. The package wrapper is always the directory in which the class lies, using dot syntax.
eg. myFlexRIA.mx.fb3 would be inside myFlexRIA\mx\fb3 folder structure.
Each class would be declared using a access modifier, the class keyword, and every function and variable directly inside the class would also need an access modifier declared by their respectable key words.
Access Modifiers:
public – Given function or variable is accessible from outside the class
protected – Given function or variable is only accessible to other functions inside the class, and to any child classes.
private – Given function or variable is only accessible to member within the same class
internal – Given function or variable is accessible to members inside the same class and inside the same package
So, now we have established what a package is, and how to declare a class and its contained functions, below you can see an example class.
package myFlexRIA.mx.FB3 { //Declare my package and its namespace public class objectPlay //Declaring my class mathPlay { public var availOut:String; private var availIn:String; public function objectPlay() //this is my class constructor { /* *Class constructor function is *run whenever an instance of *this class is created. The constructor's *name is always the same as the *name of the class */ loadFunctionOne(); //calling a function declared inside this class
availIn = 'This is Only Avaiable inside this class.'; //assign a value availOut = 'This is avaiable inside or outside of this class.'; //assign a value } private function loadFunctionOne() //A function only avaiable inside this class { trace('Welcome!'); } } }
To create an object from out class we simply use the following format.
import myFlexRIA.mx.FB3.objectPlay; //tells the AS compiler where to find the class we will be using. var myObject:objectPlay = new objectPlay(); //declare a variable of type mathPlay. //the varible type is always the name of the class that will be used. //now myObject variable contains an instance of mathPlay() myObject.availOut = 'Hello Inthere!'; //since availOut has a public Access Modifier I am able to set its //value from outside the class. The same can not be done with private //methods and properties
Now we have seen how to declare a class and its members, inside of a package. Below are some useful links of site I found helpful while trying to learn.
Links:
AdobeLabs - Packges & Namespaces