Beginning ActionScript 3.0 – Class 2

Coins-128x128 In part 1 of class section(Beginning ActionScript 3.0 – Class 1), we have got some basic ideas about the package and the class attributes. So, we can write the skeleton of the Author class.The code is as below.

clip_image002

Ok, the next step is the class body.

The class body, which is enclosed by curly braces, is used to define the variables, constants, and methods of your class.

Eh, show you the full version of Author class now.

package {
public class Author {

private var newEmail:Boolean = false;
private var articleFinished:Boolean = false;

public function haveBreakfast():void {
trace(”have breakfast”);
}

public function practiceCalligraphy() {
trace(”practice calligraphy”);
}

public function checkEmail() {
trace(”check the email”);

if (newEmail) {//selection
trace(”Reply the email”);
} else {
trace(”Read blogs”);
}
}

public function writeArticle() {
while (!articleFinished) {//repetition
trace(”Busy writing”);
articleFinished = true;
}
}
}
}

Compare to the NON-OOP style code, what you get now?

At first, we define two variables in the beginning of the code.

private var newEmail:Boolean = false;

private var articleFinished:Boolean = false;

Defining the variables inside a class body is just the same as you do in the main program.

Then, we define some methods.

public function haveBreakfast():void {
trace(”have breakfast”);
}

Defining the methods is just the same as you defining the functions in the main program.

Eh, you may find a little difference in defining the variables and methods with defining the variables and functions outside the class body; we use “private” to modify the variable and “public” to modify the method. What is the meaning of “private” and “public” here?

In fact, “private” and “public” is the class property attributes.

Attributes

The variables, constants and methods in the class body, are all called the properties of the class. Before we discuss the class property, let’s take a look at the class property attributes.

In discussions of the ActionScript object model, the term property means anything that can be a member of a class, including variables, constants, and methods. This differs from the way the term is used in the ActionScript 3.0

Language and Components Reference, where the term is used more narrowly and includes only class members that are variables or are defined by a getter or setter method. In ActionScript 3.0, there is a set of attributes that can be used with any property of a class. The following table lists this set of attributes.

Attribute Definition

l internal (default) Visible to references inside the same package.

l private Visible to references in the same class.

l protected Visible to references in the same class and derived classes.

l public Visible to references everywhere.

l static Specifies that a property belongs to the class, as opposed to instances of the class.

l UserDefinedNamespace Custom namespace name defined by user.

In general, we use the keyword “private” to modify the variables. So, the variables can’t be access outside the class body, the variable is invisible outside the class. And the definition of methods often start with “public”, that’s because we need to let the methods be accessed outside the class.

Using the class property attributes can form some access rules of your class hierarchy, how to use the attributes depends on your need.

Ok, after we finished the Author class, we need to save it. Remember, save this file in the same directory of you FLA file, and the file name should be the class name with postfix “.as”, in this example is “Author.as”.

Try to write you class now :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

Leave a Reply