Flash CS3 Documentation |
|||
| Programming ActionScript 3.0 > Getting started with ActionScript > Creating your own classes > Writing the code for a class | |||
Once you have a design plan for your class, or at least some idea of what information it will need to keep track of and what actions it will need to carry out, the actual syntax of writing a class is fairly straightforward.
Here are the minimum steps to create your own ActionScript class:
class statement to define the name of the class. To do this, enter the words public class, and then the class's name, followed by opening and closing curly braces that will surround the contents of the class (the method and property definitions). For example:
public class MyClass
{
}
The word public indicates that the class can be accessed from any other code. For other alternatives, see Access control namespace attributes.
package statement to indicate the name of the package in which your class will be found. The syntax is the word package, followed by the full package name, followed by opening and closing curly braces (which will surround the class statement block). For example, we'd change the code in the previous step to this:
package mypackage
{
public class MyClass
{
}
}
var statement within the class body; the syntax is the same as you use to declare any variable (with the addition of the public modifier). For example, adding these lines between the opening and closing curly braces of the class definition will create properties named textVariable, numericVariable, and dateVariable:public var textVariable:String = "some default value"; public var numericVariable:Number = 17; public var dateVariable:Date;
myMethod() method, enter:
public function myMethod(param1:String, param2:Number):void
{
// do something with parameters
}
public function MyClass()
{
// do stuff to set initial values for properties
// and otherwise set up the object
textVariable = "Hello there!";
dateVariable = new Date(2001, 5, 11);
}
If you don't include a constructor method in your class, the compiler will automatically create an empty constructor (one with no parameters and no statements) in your class.
There are a few more class elements that you can define.These elements are more involved.
Flash CS3
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000033.html
Comments
philggg said on Apr 22, 2007 at 2:09 PM :