Beginning ActionScript 3.0 – Constructor

Help-and-support-256x256 When we introduce the basic OOP concepts, we use an example to illustrate the OOP-style code(@see Beginning ActionScript 3.0 – Introduction to OOP). Inside the example, we use the new operator. Did you still remember the function of “new” operator?

Constructor


We said the “new” operator calls the constructor. Now, we’ll talk about the constructor.

Constructor methods, sometimes simply called constructors, are functions that share the same name as the class in which they are defined. Any code that you include in a constructor method is executed whenever an instance of the class is created with the new keyword.

Go back to the statement.

authorInstance = new Author();

Focus on the parentheses.

Due to our previous experience the appearance of parentheses always means the function call, isn’t it?

So, here, it looks like we call a method name Author, if we ignore the new operatorJ

Now, here comes the first part of constructor, the name.

The name of the constructor should the same with the class name.

Let’s try to add a constructor to the Author class.

Add the following code to the Author class.

function Author(){
trace(”The constructor of Author class”);
}

It’s just a simple function with the same name of the class.

Run the code, and you’ll get:

clip_image002

The code runs smoothly.

Compare the method definition to other methods defined inside the class body, you can easily find out that:

Firstly, the constructor doesn’t have any attributes, such as internal or private. You can only use the “public” attribute optionally, but you can’t use any other attributes to modify the constructor. So, you can ignore the attribute, when you write the constructor.

Secondly, this method has no return declaration, a data type follow by the colon. The official document says, although it is legal to use the return statement inside a constructor, it is not permissible to return a value.

Why?

Consider the situation when we use the constructor.

authorInstance = new Author();

After the constructor has been called, it should return a corresponding reference to the object, here is the Author type reference. Then the reference will be assign to the authorInstane here. So, if ActionScript allows you to return a value inside the constructor, it’ll get confused. We only expect an object reference to the corresponding class, not other things. And the return of object reference is better to handle by the compiler or runtime environment.

Please remember, don’t use the return statement inside the constructor.

Like other methods, the constructor can have parameters while you can’t call the constructor like you call other methods.

In general, the constructor will be call only once for an object, at the beginning of its lifecycle. So, in most cases, we put the initialization work inside the constructor. But, don’t try to initialize the static variables inside the constructor, because you can use the static variable before you initialize it in this situation.

We know the Author class is inherited from the Person class now.

Aha, next experiment now.

Add some code to the Person class, eh, just add a constructor to the Person class.

function Person(){
trace(”a new person was born”);
}

Yeah, typical constructorJ

And run the test code again now, and you’ll get the following result.

clip_image004

The output means that when you call a constructor of a subclass, the constructor of the subclass will call its parent’s constructor. And this is won’t stop until it reaches the root of this inheritance hierarchy, the Object.

OK, all for this topic

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.

3 Responses to “Beginning ActionScript 3.0 – Constructor”

  1. marliton says:

    Hi,

    How you keep the code format in this post? It’s a plug-in?

  2. Ntt.cc says:

    @marliton

    Yes, a plugin named coolcode

  3. [...] the last part of Constructor(@see Beginning ActionScript 3.0 – Constructor), we do a simple experiment. And the experiment shows that the constructor of the superclass will [...]

Leave a Reply