-
Flash - --- ->>
Creating a custom class 119
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);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks