开发者

Is there a way to subclass and override a method that is in a custom namespace?

开发者 https://www.devze.com 2023-02-19 07:09 出处:网络
Say I have a class with a method defined in a namespace other than public, protected or internal... package com.foo.bar

Say I have a class with a method defined in a namespace other than public, protected or internal...

package com.foo.bar
{
   import com.foo.my_name_space
   public class bar
   {
    private var _vabc:String
    private var _v123:String

    protected function set123(val:String):void{
        _v123 = val;
    }

    my_name_space function setABC(val:String):void{
        _vabc = val;
    }

   }
}

Now I want to extend and override this in a subclass...

package com.foo
{
   import com.foo.bar.bar
   import com.foo.my_name_space

   public class foo extends bar
   {
      override protected function set123(val:String):void{
        super.set123(val);
      }

       .... ????? ...

   }
 }

Easy enough to override protected, public etc. methods, but is there a way to override the setABC method defined in the name space *my_name_space* ?

I've tried the following syntax, which seems to pass the FlashBuilder pre-compiler check but doesn't work.

    use namespace my_name_space override function my_name_space::setABC(val:String):void
开发者_Python百科

I've tried a number of other syntax combinations but most wouldn't even pass the pre-compile check. (many with some type of namespace error) I have a feeling this isn't possible but wonder if anyone might have any ideas?


James' answer is right. You just failed to add the use namespace directive appropriately. This example works as expected:

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import test.Base;
    import test.Child;
    import test.my_namespace;

    public class Main extends Sprite {

        use namespace my_namespace;

        public function Main():void {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            var base:Base = new Base();
            base.test();

            var child:Child = new Child();
            child.test();
        }

    }

}

package test {
    import test.my_namespace;
    use namespace my_namespace;

    public class Base {

        my_namespace function test():void {
            trace("Base");
        }

    }

}

package test {
    import test.Base;
    import test.my_namespace;
    use namespace my_namespace;

    public class Child extends Base {

        public function Child() {

        }

        override my_namespace function test():void {
            trace("Child");
            super.my_namespace::test();
        }
    }

}

package test {
    public namespace my_namespece;
}


This should work

override my_name_space function setABC(val:String):void
{   
}
0

精彩评论

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