开发者

New to AS3: Constructor Classes

I was reading a tutorial on creating a AS3 class. I'm confused by constructors - they are the code executed when you开发者_高级运维 create a new instance of the class, right? However, this part:

public function Greeter(initialName:String = "") {
name = initialName;
}
public var name:String;

The public var name:String part is not part of the constructor, but shouldn't it be in front of where the name variable is set?

Also, second question, when is the public var name:String; part executed? Functions are executed when some code calls it. and the constructor is executed when a new instance of a class is created. But what about code not in any of those?

Thanks!


In your example, you are showing something called a member variable. A member variable is something which is declared as a var between class <ClassName>{ and the corresponding } (that area is the "body of the class"). While technically it can appear anywhere in the body of the class, it is most often found at the very top.

The space on the heap for these variables will be allocated when the instance class is constructed. So, in your case, in addition to the raw object being created in memory when you call new Greeter(), a special place for name is also included. In your particular case, the constructor also assigns a value to that property, but it is also possible for the property to be null.

Every var not marked static will be a member variable. That means that it is a specific property of that variable instance. This means that you can have 50 Greeters which all have different name's -- they can all have different member variable values. On the other hand, if you have static var name, that means that you can't simply access a Greeter's name, but you have to access the value Greeter.name. While inside the class (and the children of the class), you'll be able to access name as a sort of local variable, the rest of the world can't simply do:

var g:Greeter = new Greeter(); 
g.name = "foo"; //this would cause an error

Instead, they need to address it as a property of the class:

Greeter.name = "foo"; 
var g:Greeter = new Greeter(); 
trace(g.hasOwnAttribute("name")); //false


The AS compiler will automatically reorganize your code. In reality, it will appear more like this:

public var name:String;
public function Greeter(initialName:String = "") {
    name = initialName;
}

You can test this by doing the following:

public var name:String = "Hello!";
public function Greeter(initialName:String = "") {
    trace(name); // outputs Hello!
}

This too, will work:

public function Greeter(initialName:String = "") {
    trace(name); // outputs Hello!
}
public var name:String = "Hello!";

All code that resides in the class, but outside of a method (including the constructor, which is simply a special method) will be executed prior to constructor code, which can be a little misleading.

In general though, don't do anything outside of methods except at most setting variables like above. Normally all setting up should be done within the constructor.

EDIT

I'm updating my answer so the OP get's the correct information even though another answer has been accepted.

The compiler does recompile code. See below images - I am using the SoThink SWF decompiler which shows the state of the class file after the SWF has been published. Notice how the line is brought up, and so it is actually reorganised so it exists at the top of the class.

The Flash Document calling the class

New to AS3: Constructor Classes

The class code (note the variable at the bottom)

New to AS3: Constructor Classes

The decompiled SWF class code (note the variable at the top) Out of interest, also note how Flash internally changes variable names, such as param1, etc

New to AS3: Constructor Classes

If you have sothink decompiler you can try this for yourself.


I believe in Action Script it doesn't matter where in the class variables are declared. While mostt C style languages including AS don't require to declare varibles prior to use, Pascal style languages do force you to do that.

The var name is visible to all methods of the Greeter classe, but if it was declared inside the constructor (a local variable) it would have only been available within the scope of the constructor. If you declare a local variable then it's not accessible to other members of the class. In this case var name is declared as a class variable which is intended to be visible to all members of the class (by members I mean methods and constructors and properties). Furthermore because it's a public class variable then it's accessible to other code that references an instance of the Greeter class.

Most compilers initialize primitive variables (eg. Booleans to false and Ints to 0). Non-primitive types however are left as null. You should consider constructors as some logic that prepares a new instance of an object for use. It shouldn't usually execute other code besides initialization.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜