开发者

setting actionscript 3 superclass variables

开发者 https://www.devze.com 2022-12-26 06:28 出处:网络
In AS3, if I have a class such: public class dude { //default value for a dude protected var _strength:Number = 1;

In AS3, if I have a class such:

public class dude
{
//default value for a dude
protected var _strength:Number = 1; 
public function dude( ):void
{    super( );
     //todo... calculate abilities of a dude based on his strength.
}
}

and a subclass

public class superDude extends dude
{

public function superDude( ):void
{
   _strength = 100;
   super( );
   trace( "strength of superDude: " + _strength );
}
}

Th开发者_开发技巧is will trace strength of superDude is 1. I expected the variable I set in the subclass (prior to calling the superclass constructor) to remain.

Is there a way to assign class variables in subclass constructors which are not over-written by the superclass construtor? Or should I pass them up as constructor variables?


You are calling super() after setting _strength to 100; this instruction is going to call the constructor from the super class which is going to change back _strength to 1 (the variable initialization happens in constructor). There is no way to prevent that and I think you should call super() before initializing the variables.


First off.. the default super class constructor (that does not take any parameters) is implicitly called from the subclass constructor. So you don't need to call it explicitly.

If the superclass constructor takes parameters in then you need to call it explicitly and that call must be the very first statement in the subclass constructor.


In response to Kayes answer, I must mention that "super" should NOT always be called first.

Ideally, constructor parameters should be copied to instance variables of the current class ASAP (i.e. before calling super), because they may need to be available to an overridden method that may be called by the super class constructor. If those variables are not initialized before super is called, then they won't be available to the overridden method. On the other hand, super should be called before you need to access any stage instances.

So it's correct to initialize variables of the current class before calling super, but it's incorrect to initialize public/protected variables of the super class in the subclass before calling super, because they'll be reset when super is called.

public class GUIControlSubclass extends GUIControl
{
    private var _param:String;
    public function GUIControlSubclass( param:String )
    {
        _param = param; //variable of this class must be assigned before calling super, so it's available to override of initLayout
        _backgroundColor = 0xffffff; //it is incorrect to assign protected/public variable of super class here, since it will be reset by super call
        super(); //super class constructor will call initLayout, which will call this subclass's override of it; super class constructor itself should call it's own "super" method *before* calling initLayout, to ensure stage instances are constructed and available to initLayout
    }

    override protected function initLayout():void
    {
        super.initLayout();
        //IF _param WAS NOT SET **BEFORE** CALLING SUPER IN THE CONSTRUCTOR, ITS VALUE WOULD BE NULL HERE, SINCE SUPER TRIGGERS THIS METHOD, THEREFORE _param MUST BE ASSIGNED BEFORE SUPER IS CALLED
    }
}
0

精彩评论

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

关注公众号