English 中文(简体)
calling super() from an actionscript constructor with varargs
原标题:

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?

最佳回答

There s unfortunately no way to call the super constructor with ... args. If you remove the super() 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:

override public function doSomething(arg1:Object, ...args):void {
  switch(args.length) {
    case 0: super.doSomething(arg1); return;
    case 1: super.doSomething(arg1, args[0]); return;
    case 2: super.doSomething(arg1, args[0], args[1]); return;
  }
}




相关问题
UserControl constructor with parameters in C#

Call me crazy, but I m the type of guy that likes constructors with parameters (if needed), as opposed to a constructor with no parameters followed by setting properties. My thought process: if the ...

Strange "type class::method() : stuff " syntax C++

While reading some stuff on the pImpl idiom I found something like this: MyClass::MyClass() : pimpl_( new MyClassImp() ) First: What does it mean? Second: What is the syntax? Sorry for being such ...

Anonymous class question

I ve a little doubt over this line: An anonymous class cannot define a constructor then, why we can also define an Anonymous class with the following syntax: new class-name ( [ argument-list ] ) {...

Why copy constructor is not called in this case?

Here is the little code snippet: class A { public: A(int value) : value_(value) { cout <<"Regular constructor" <<endl; } A(const A& other) : value_(other....

Invoking an instance method without invoking constructor

Let s say I have the following class which I am not allowed to change: public class C { public C() { CreateSideEffects(); } public void M() { DoSomethingUseful(); } } and I have to call M ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

热门标签