[ Pobierz całość w formacie PDF ]
Creating an object from a class 117
Creating a custom class
While ActionScript includes many classes of objects, such as the MovieClip class and the Color
class, there will be times when you need to construct your own classes so you can create objects
based on a particular set of properties or methods.
To create a class that defines each of the new objects, you create a constructor for a custom object
class and then create new object instances based on that new class, as in the following example:
Note: The following ActionScript is an example only. You should not enter the script in your lesson
FLA file.
function Product (id:Number, prodName:Name, price:Number)
{
this.id:Number = id;
this.prodName:String = prodName;
this.price:Number = price;
}
In order to properly define a class in ActionScript 2.0, you must surround all classes by the class
keyword, and you must declare all variables in the constructor outside of the constructor.
Following is an example:
Note: The following ActionScript is an example only. You should not enter the script in your lesson
FLA file.
class Product
{
//variable declaration
var id:Number
var productName:String
var price:Number
//constructor
function Product (id:Number, prodName:Name, price:Number)
{
this.id = id;
this.prodName = prodName;
this.price = price;
}
}
To create objects from this class, you could now use the following code:
Note: The following ActionScript is an example only. You should not enter the script in your lesson
FLA file.
var cliplessPedal:Product=new Product(1, "Clipless Pedal", 11);
var monkeyBar:Product=new Product(2, "Monkey Bar", 10);
However, in ActionScript 2.0, variables that are part of a class structure should not be accessed
directly. You should write methods within the class that will access these variables directly. There
should be different methods that get and set properties (known as getter and setter methods).
You must indicate the data type for both a method s return value and any parameters that are
passed to the method when it is called.
118 Chapter 15: Work with Objects and Classes Using ActionScript 2.0
Specify the data type for method return values
You must indicate data types for values returned by methods after the method name and list of
parameters, as in the following example:
Note: The following ActionScript is an example only. You should not enter the script in your lesson
FLA file.
public function getProductName() :String
{
return name;
}
If no value is returned (for example, a property is being set), the data type is Void:
public function setProductName(productName:String) :Void
{
this.productName=productName;
}
Build a custom class
You ll now build a new Product class with getter and setter methods and create an object from the
Product class.
1 Create an ActionScript file by doing one of the following:
% If you re using Flash MX 2004 Professional, select File > New > ActionScript File (Not Flash
Document). Save the document with the name Product.
% If you re using Flash MX 2004, open a text editor, such as Notepad. Save the file
with the name Product.as. (Remember to give the file the AS extension, to create an
ActionScript file.)
2 Create a constructor for a Product class by creating a function called Product that takes the
arguments id, prodName, and description:
function Product (id:Number, prodName:String, description:String)
{}
3 In the constructor function, set the properties of the Product class equal to the setter methods
that you will create:
setID(id);
setProdName(prodName);
setDescription(description);
4 Wrap the constructor function around the class keyword. Be sure to declare each variable used
in the class:
class Product
{
var id:Number;
var prodName:String;
var description:String
function Product (id:Number. prodName:String, description:String)
{
setID(id);
setProdName(prodName);
setDescription(description);
}
}
Creating a custom class 119
5 Define getter and setter methods for each property of the class, as in the following example. Be
sure to specify Void as the return type for the setter methods, and indicate the data type returned
for the getter methods.
class Product
{
var id:Number;
var prodName:String;
var description:String
function Product (id:Number, prodName:String, description:String) {
setID(id);
setProdName(prodName);
setDescription(description);
}
public function setID (id:Number) :Void
{
this.id = id;
}
public function setProdName (prodName:String) :Void
{
this.prodName = prodName;
}
public function setDescription (description:String) :Void
{
this.description = description;
}
[ Pobierz całość w formacie PDF ]