开发者

ActionScript - Overriding Method Without Matching Signature?

when extending a class, is it impossible to override a method开发者_如何学Python without also matching the parameters?

for example, i'd like to use the method's name, in this case it's a socket extension and the method i want to override is connect. however, i want to request additional parameters that the stock connect function does not request.

is the only alternative to create my own connect-like method with my own parameters, call super.connect from this function and override the stock connect function to throw an error if it's called?

that all kind of sounds like a train wreck.


Function overloading is not supported in ActionScript (however Darron Schall demonstrated some kind of pseudo overloading in this article).

I think in your case it's only left over to create your own connectEx method.


Sadly, overloading is not supported. As a next best option, you could consider optional arguments. This would allow you to pass as few or as many params as you want to a method. The method would receive these params in an array and you could handle them however you want from that point on.

Here's how a method using optional params would look:

function someMethod(...params):void
{
    for(var i:int = 0 ; i < params.length ; i++ )
    {
        trace("parameter: " + params[i]);
    }
}

You can read more about optional parameters here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜