2 Replies Latest reply on Oct 4, 2008 3:20 AM by Peter Lorent

    Extending a class

    SiHoop Level 1
      I'm having a problem creating subclasses. I thought I could extend any class using code as below. I thought this would create a new method: doThis.

      Then, to use the code, I thought I had to import the subclass, create an instance of the class, then use the new method. When I do this, I get the following message, suggesting that my subclass has a big error. What am I missing?

      Implicit coercion of a value with static type Array to a possibly unrelated type com.simon.projects:ExtendArray. GT6/src GT6.mxml line 56 1223076140502 5872
        • 1. Re: Extending a class
          Level 7

          "SiHoop" <webforumsuser@macromedia.com> wrote in message
          news:gc69t9$5er$1@forums.macromedia.com...
          > I'm having a problem creating subclasses. I thought I could extend any
          > class
          > using code as below. I thought this would create a new method: doThis.
          >
          > Then, to use the code, I thought I had to import the subclass, create an
          > instance of the class, then use the new method. When I do this, I get the
          > following message, suggesting that my subclass has a big error. What am I
          > missing?
          >
          > Implicit coercion of a value with static type Array to a possibly
          > unrelated
          > type com.simon.projects:ExtendArray. GT6/src GT6.mxml line 56
          > 1223076140502 5872
          >
          >
          > package {
          > public class ExtendArray extends Array{
          > public function ExtendArray(){
          > }
          > public function doThis():void{
          > trace("hello")
          > }
          > }
          > }
          >
          > Usage:
          > import ExtendArray;
          > public var myArray:ExtendArray=[]
          > myArray.doThis()

          First, you need to make sure your constructor signature matches Array. I,
          personally wouldn't take on extending Array because Array somehow has two
          constructors, despite the fact that ActionScript is not supposed to allow
          method overloading. Instead, I'd composite array into whatever class makes
          most sense to extend (EventDispatcher might be a good one, depending on what
          you are trying to achieve. This will give you a "has a" relationship
          instead of an "is a" relationship.

          So maybe something like:

          package foo {
          //make it possible to get to the array without dotting down:
          [DefaultProperty("exArray")]
          public class ExtendArray {
          //if you pass it a different array, this will get overwritten with
          the
          //new one, but this gives us a blank one to start out with
          public var exArray:Array=new Array();
          public function ExtendArray(target:IEventDispatcher=null){
          super(target);
          }
          public function doSomething(){
          trace('doing something now');
          }
          }
          }

          I've never used a default property with an object that itself has properties
          and methods, but in theory (according to my understanding), you should be
          able to do this:

          public var myArray:ExtendArray=[];
          myArray.push('hi');
          myArray.doSomething();

          even though those functions actually belong to two different levels, because
          anything not specifically routed will go to the default property.

          HTH;

          Amy


          • 2. Re: Extending a class
            Peter Lorent Level 2
            You get the error because you use the array notation to initialize your custom array.
            Use public var myArray:ExtendArray=new ExtendArray(); to initialize.
            And make the custom array dynamic...