有没有一种方法,以继承和覆盖方法是在自定义命名空间? [英] Is there a way to subclass and override a method that is in a custom namespace?
问题描述
说我有一类与公众相比其他命名空间中定义的方法,保护或内部...
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);
}
.... ????? ...
}
}
很容易来覆盖保护,公众等方法,但有没有办法覆盖的名字空间中定义的setABC方法* my_name_space *?
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* ?
我试过下面的语法,这似乎传递FlashBuilder pre-编译器检查,但无法正常工作。
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
我已经尝试了一些其他的语法组合,但大多数人甚至不通过pre-编译检查。 (多用某种类型的命名空间的错误)我有一种感觉,这是不可能的,但不知道是否有人可能有什么想法?
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;
}
这篇关于有没有一种方法,以继承和覆盖方法是在自定义命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!