从带有可变参数的动作脚本构造函数调用 super()
如果构造函数将其参数作为可变参数(...),则似乎不可能创建一个仅将该可变参数传递给超类的子类。
对于正常功能,有一个相关的问题可以解决相同的情况:Wrapping a Vararg Method in ActionScipt 但我无法让它与超级调用一起工作。
基类:
public class Bla
{
public function Bla(...rest)
{
trace(rest[0]); // trace the first parameter
}
}
子类:
public class Blie extends Bla
{
public function Blie(...rest)
{
// this is not working, it will
// pass an array containing all
// parameters as the first parameters
super(rest);
}
}
如果我现在调用
var b1 = new Bla('d', 'e');
var b2 = new Blie('a', 'b', 'c');
我得到输出
d
a,b,c
并且我希望它打印出来:
d
a
除了实际将参数的处理移至子类或将其移至单独的初始化方法之外,有谁知道如何获取超级打电话对吧?
If a constructor takes its parameters as a vararg (...) it seems to be impossible to create a subclass that will just pass on that vararg to the superclass.
There is a related question with fix for this same situation for normal functions: Wrapping a Vararg Method in ActionScipt but I cannot get that to work with a super call.
base class:
public class Bla
{
public function Bla(...rest)
{
trace(rest[0]); // trace the first parameter
}
}
subclass:
public class Blie extends Bla
{
public function Blie(...rest)
{
// this is not working, it will
// pass an array containing all
// parameters as the first parameters
super(rest);
}
}
if I now call
var b1 = new Bla('d', 'e');
var b2 = new Blie('a', 'b', 'c');
I get the output
d
a,b,c
And I want it to print out:
d
a
Aside from actually moving the handling of the parameters to the subclass or shifting it off to a separate initializer method, does anyone know how to get the super call right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,无法使用
...args
调用超级构造函数。如果删除super()
调用,它将由编译器调用(不带参数)。arguments
也无法从构造函数访问。如果您可以更改方法签名,则可以修改参数以接受
Array
而不是...args
。否则,正如您所提到的,您可以将其移至初始值设定项方法中。There's unfortunately no way to call the super constructor with
... args
. If you remove thesuper()
call, it will be called by the compiler (with no arguments).arguments
is also not accessible from constructors.If you can change the method signatures, you modify the arguments to accept an
Array
rather than... args
. Otherwise, as you mentioned, you could move it into an initializer method.您可以使用这样的语句:
You may use a statement like this: