Classes in ActionScript 3
       by kirupa  |  24 December 2008

To give you an idea of the steps needed to create a class and tie it with an application, the previous page took you on a quick run of the steps. In this and the following pages, we will slow down and look at what you did in the previous page in greater detail.

The Structure of a Class in AS3
While a class can contain a variety of data, all classes share a certain structure that allows the Flash Compiler to know what to do when you build your application. Let's look at the contents of our Name file to learn more:

package
{
public class Name
{
private var firstName:String;
private var lastName:String;
 
function Name()
{
 
}
 
public function SetFirstName(val:String):void
{
firstName = val;
}
 
public function SetLastName(val:String):void
{
lastName = val;
}
 
public function GetFullName():String
{
return firstName + " " + lastName;
}
}
}

The first thing your class needs is a package declaration:

package
{
public class Name
{
private var firstName:String;
private var lastName:String;
 
function Name()
{
 
}
 
public function SetFirstName(val:String):void
{
firstName = val;
}
 
public function SetLastName(val:String):void
{
lastName = val;
}
 
public function GetFullName():String
{
return firstName + " " + lastName;
}
}
}

For the sake of simplicity, think of a package as a giant folder that links various classes together. Because we only have one class in our application, the name of the package does not particularly matter. Despite this not mattering, you still need the package keyword in order to build your application.


Next up is the class definition:

package
{
public class Name
{
private var firstName:String;
private var lastName:String;
 
function Name()
{
 
}
 
public function SetFirstName(val:String):void
{
firstName = val;
}
 
public function SetLastName(val:String):void
{
lastName = val;
}
 
public function GetFullName():String
{
return firstName + " " + lastName;
}
}
}

The class definition tells Flash what the name of your class will be and what type of access restrictions it will have. The main thing to note is the class keyword that precedes the name of your Name class.

Preceding the class keyword, you see the word public displayed. The public keyword is something that is known as an access modifier, and it describes how accessible this particular class is. I won't describe access modifiers in great detail in this article, so just note that this value is commonly public (as in this example) or private.


Next up are two things that are probably familiar to you, and they are variables:

package
{
public class Name
{
private var firstName:String;
private var lastName:String;
 
function Name()
{
 
}
 
public function SetFirstName(val:String):void
{
firstName = val;
}
 
public function SetLastName(val:String):void
{
lastName = val;
}
 
public function GetFullName():String
{
return firstName + " " + lastName;
}
}
}

Variables allow you to store data inside them, and the names of our variables are firstName and lastName. All variables in ActionScript are declared using the var keyword.

What you can also do is specify the type of content your variable will be storing by following the variable name with a colon and the name of the type. In this case, both our variables will be storing data of type String, so I specify that after the variable name.

Finally, you will see both of our variables starting off with the private keyword. Just like the public keyword you saw earlier as part of our class definition, the private keyword is another access modifier.


Next up is a function whose name is the same as that of our class:

package
{
public class Name
{
private var firstName:String;
private var lastName:String;
 
function Name()
{
 
}
 
public function SetFirstName(val:String):void
{
firstName = val;
}
 
public function SetLastName(val:String):void
{
lastName = val;
}
 
public function GetFullName():String
{
return firstName + " " + lastName;
}
}
}

This function is known as a constructor. I will explain what a constructor does in a little bit, but to give you a sneak preview, a constructor is responsible for setting the spark that creates the object this class is the template for.


The last things we are going to look at are functions (also known as methods):

package
{
public class Name
{
private var firstName:String;
private var lastName:String;
 
function Name()
{
 
}
 
public function SetFirstName(val:String):void
{
firstName = val;
}
 
public function SetLastName(val:String):void
{
lastName = val;
}
 
public function GetFullName():String
{
return firstName + " " + lastName;
}
}
}

In a nutshell, a function is a block of code that does something when asked to. It may just do something and stop, or it may do something and return some data back to whatever asked it.

In our class, we have three functions called SetFirstName, SetLastName, and GetFullName. Based on the names of these functions, what they do should be pretty straightforward, and the code is very simple as well.

Instead, let's focus on the structure of a typical function by looking at SetFirstName:

public function SetFirstName(val:String):void
{
firstName = val;
}

Let's read the first line from left-to-right and stop at the interesting keywords. Just like variables and class definitions, functions too have access modifiers. This function's access level is public.

A function isn't quite a function without it being defined as one, so the next keyword to look at is the word function itself. This keyword defines this whole block of code as a function.

After that, you have the function name and any variables it takes in as an argument. Our function is called SetFirstName, and it takes just one variable as its argument - a variable called val whose type is String.

The final thing to note is that the entire function declaration is followed by a return type. This means that, if your function is going to be returning any data, the data's type (class) needs to be specified. Because the SetFirstName function isn't returning anything, its return type is set to void.

An example of a function that does return some data is GetFullName:

public function GetFullName():String
{
return firstName + " " + lastName;
}

Notice that in the function declaration, the return type is set to String. When you specify a return type, Flash will expect this function to return data whose type matches the return type specified.

In this case, we are returning data in the form of a string by combining the data from the firstName and lastName variables, and that is good because that is what our function declaration indicates as well.

Ok, so you created a class and got a good look at the interesting parts of it. In the next page let's look into using this class in our application.

Onwards to the next page.


1 | 2 | 3 | 4




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.