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

蓝咒 提交于 2019-12-23 18:04:27

问题


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

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?


回答1:


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;
}



回答2:


This should work

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


来源:https://stackoverflow.com/questions/5422544/is-there-a-way-to-subclass-and-override-a-method-that-is-in-a-custom-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!