开发者

conceptual issue in inheritence property of actionscript-3

开发者 https://www.devze.com 2023-04-11 20:52 出处:网络
say, Child class is inheriting Father class and Father class is i开发者_C百科nheriting spark TextArea class. now from an mxml file (in FLEX4), i am creating multiple objects of Child class. Father cla

say, Child class is inheriting Father class and Father class is i开发者_C百科nheriting spark TextArea class. now from an mxml file (in FLEX4), i am creating multiple objects of Child class. Father class have few static attributes whose values are set by private methods, calling from constructor. Now the question is: all these static attributes are set every time while Child class objects are being created one by one?

If answer is yes then Is it possible that Father class static attributes are set only once and not depends upon the number of Child class objects creation.

Please provide any suggestion or tips

Thanks in advance.


If you are setting static variables from an object's constructor or methods called from the constructor, then yes, they will be set every time. In order to prevent that, just check whether the variable is already set.

public class Foo {

    public static var bar:Object;

    public Foo(value:Object) {
        if (!bar) {
            bar = value;
        }
    }
}


First decide if those static members are really all that important to store as statics because statics are associated with a Class and not an instance it's usually a signal that you're probably doing something you shouldn't if instances are modifying or reading static members. You probably should use a factory method if you need to share that information with the instances. However, if you're sure you should do it then you can use a static initializer block to initialize the members when the class is loaded. Downside is that block throws an exception it can be hard to track down:

public class SomeObject {
   private const _someStaticMember : String;
   private const _someOtherStaticMember : SomeOtherObject;

   static {
       _someStaticMember = "foobar";
       _someOtherStaticMember = new SomeOtherObject();
   }

}
0

精彩评论

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

关注公众号