/* Open a new FLash file (ActionScript 3.0). Select TimeLine --> Top Layer --> Frame 1 Select Window --> Actions (F9) Copy and paste this snippet */ // This program illustrates using getters and setters in classes // Using constants, getters, and setters as shown is important because // the designer of the class can make improvements to the class without // worrying that the using programmer will be impacted. Moreso, the // using programmer can't sneak inside the class and mess things up. trace ("entering program"); import Example; var myExample:Example = new Example(); trace("ID is " + myExample.getId() ); myExample.setId("changed it") ; trace("ID is " + myExample.getId() ); trace("Const is " + myExample.EXAMPLE ); trace ("leaving program"); /* ******************************************************** */ /* Open a new .as file (ActionScript 3.0). Copy and paste this snippet. Create a new folder on your desktop. Save both the .fla and .as in the folder. Save the .as file as Example.as Observe the output */ // Package below /* Let's create another class. Lets call this one Example. Here we create our own methods to be used as needed. Notice we still have to follow all the "rules" about case, etc Note: in this example we use a "getter" and a "setter" to manipulate the private variable. This is the prefered way because we can enforce rules that we may have. */ package { public class Example { public const EXAMPLE:String = "example"; private var _id:String; public function Example() { _id = "Example class"; } public function getId():String { return _id; } public function setId(newID:String):void { _id = newID; } } } // Broken?? Make sure both .fla and .as live in the same folder. // Are you a Java or C++ coder? If so you are asking where is the // destructor. ActionScript doesn't use them. Wait for 4.0, I'll bet // they're there. Too important for garbage collection. // ActionScript 3.0 Cookbook, by Joey Lott, et al; O'Reilly