开发者

Differences between variable instantiation outside constructor and in

开发者 https://www.devze.com 2023-01-31 12:05 出处:网络
I have to decide whether to put a variable within a constructor or outside, but I keep getting the feeling that I am missing information, I have looked at other posts on stack overflow, but all mentio

I have to decide whether to put a variable within a constructor or outside, but I keep getting the feeling that I am missing information, I have looked at other posts on stack overflow, but all mentioned it was a matter of preference, yet I found two difference that I feel might be important:

-If I decide to put the variables within a constructor, then I must ha开发者_开发问答ve an object parameter for any function that wish to alter the variables, even if the code is internal to the class.

-Subclassing would cause the variables to not appear, something that causes problems when the class and any subclasses must have the variables in order to operate properly.

I may be wrong on all of these points, but at 4am, I would rather be told I am wrong than commit a mistake due to pride. If this has been answered somewhere else and I missed it, I am sorry, and if you could post the link, I would be grateful.


actions inside the constructor are interpreted, all others are precompiled so they work faster


Like www0z0k already said, the declarations outside the constructor are merly interpreted, so declaring them outside can be a performance bost under some circumstances.

-If I decide to put the variables within a constructor, then I must have an object parameter for any function that wish to alter the variables, even if the code is internal to the class.

This is correct.

-Subclassing would cause the variables to not appear, something that causes problems when the class and any subclasses must have the variables in order to operate properly.

You should think about what you want to archive. Most times you should rather choose a good software design then to think about performance. This subclassing problem that you mentioned can also protect some variables from being changed.

Greetings,

iuiz


-Subclassing would cause the variables to not appear, something that causes problems when the class and any subclasses must have the variables in order to operate properly.

If I understood you correctly you're looking for protected fields (or properties).

-If I decide to put the variables within a constructor, then I must have an object parameter for any function that wish to alter the variables, even if the code is internal to the class.

Sry, I don't get that...

@Performance: with all that said about interpreted constructors you could add a simple init(...) function within your constructor which does all you would do in the constructor - but without being interpreted.

public function ConstructorOfClass(arg1:int, arg2:*)
{
    init(arg1, arg2);
}

private function init(arg1:int, arg2:*):void
{
    // do whatever you want here
}
0

精彩评论

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