有没有办法子类化并重写自定义命名空间中的方法?

发布于 2024-10-26 10:35:37 字数 978 浏览 0 评论 0原文

假设我有一个类,其方法在公共、受保护或内部以外的命名空间中定义...

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

   }
}

现在我想在子类中扩展和覆盖它...

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

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

   }
 }

很容易覆盖受保护、公共等方法,但是有吗重写名称空间 *my_name_space* 中定义的 setABC 方法的方法?

我尝试了以下语法,它似乎通过了 FlashBuilder 预编译器检查,但不起作用。

    use namespace my_name_space override function my_name_space::setABC(val:String):void

我尝试了许多其他语法组合,但大多数甚至都无法通过预编译检查。 (许多有某种类型的命名空间错误)我有一种感觉这是不可能的,但想知道是否有人有任何想法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩动你心 2024-11-02 10:35:37

詹姆斯的回答是对的。您只是未能正确添加 use namespace 指令。此示例按预期工作:

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

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;
}
我是男神闪亮亮 2024-11-02 10:35:37

这应该有效

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

This should work

override my_name_space function setABC(val:String):void
{   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文