您如何在ActionScript中超载函数?

发布于 2024-12-05 06:34:49 字数 344 浏览 0 评论 0 原文

我想要一个能够接受各种类型的函数。 AS3 不支持直接重载...所以我不能执行以下操作:

//THIS ISN'T SUPPORTED BY AS3

function someFunction(xx:int, yy:int, someBoolean:Boolean = true){
    //blah blah blah
}
function someFunction(arr:Array, someBoolean:Boolean = true){
    someFunction(arr[0], arr[1], someBoolean);
}

如何解决它并且仍然有一个能够接受各种类型参数的函数?

I want a function to be able to take in various types. AS3 doesn't support overloading directly... so I can't do the following:

//THIS ISN'T SUPPORTED BY AS3

function someFunction(xx:int, yy:int, someBoolean:Boolean = true){
    //blah blah blah
}
function someFunction(arr:Array, someBoolean:Boolean = true){
    someFunction(arr[0], arr[1], someBoolean);
}

How can I work around it and still have a function that is able to take arguments of various types?

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

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

发布评论

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

评论(2

一身仙ぐ女味 2024-12-12 06:34:49

如果您只想接受任何类型,则可以使用*允许任何类型:

function someFunction( xx:*, yy:*, flag:Boolean = true )
{
  if (xx is Number) {
    ...do stuff...
  } else if (xx is String) {
    ...do stuff...
  } else {
    ...do stuff...
  }
}

如果您有大量订单不重要的各种参数,请使用选项对象:

function someFunction( options:Object )
{
  if (options.foo) doFoo();
  if (options.bar) doBar();
  baz = options.baz || 15;
  ...etc...
}

如果您有一个可变数量的参数,您可以使用 ... (reta)参数

function someFunction( ... args)
{
  switch (args.length)
  {
    case 2:
      arr = args[0];
      someBool = args[1];
      xx = arr[0];
      yy = arr[1];
      break;
    case 3:
      xx = args[0];
      yy = args[1];
      someBool = args[2];
      break;
    default:
      throw ...whatever...
  }
  ...do more stuff...
}

对于需要将共同函数调用到许多多个多个功能的情况类,您应该指定每个类共有的接口:

function foo( bar:IBazable, flag:Boolean )
{
  ...do stuff...
  baz = bar.baz()
  ...do more stuff...
}

If you just want to be able to accept any type, you can use * to allow any type:

function someFunction( xx:*, yy:*, flag:Boolean = true )
{
  if (xx is Number) {
    ...do stuff...
  } else if (xx is String) {
    ...do stuff...
  } else {
    ...do stuff...
  }
}

If you have a large number of various parameters where order is unimportant, use an options object:

function someFunction( options:Object )
{
  if (options.foo) doFoo();
  if (options.bar) doBar();
  baz = options.baz || 15;
  ...etc...
}

If you have a variable number of parameters, you can use the ... (rest) parameter:

function someFunction( ... args)
{
  switch (args.length)
  {
    case 2:
      arr = args[0];
      someBool = args[1];
      xx = arr[0];
      yy = arr[1];
      break;
    case 3:
      xx = args[0];
      yy = args[1];
      someBool = args[2];
      break;
    default:
      throw ...whatever...
  }
  ...do more stuff...
}

For cases where you need to call a common function to a number of classes, you should specify the interface common to each class:

function foo( bar:IBazable, flag:Boolean )
{
  ...do stuff...
  baz = bar.baz()
  ...do more stuff...
}
哭了丶谁疼 2024-12-12 06:34:49

可以:

function something(...args):void
{
    trace(args[0], args[1]);
}

这样你就可以轻松地循环你的参数等(甚至检查参数类型):

function something(...args):void
{
    for each(var i:Object in args)
    {
        trace(typeof(i) + ": " + i);
    }
}

something("hello", 4, new Sprite()); // string: hello
                                     // number: 4
                                     // object: [object Sprite]

Could just have:

function something(...args):void
{
    trace(args[0], args[1]);
}

This way you can easily loop through your arguments and such too (and even check the argument type):

function something(...args):void
{
    for each(var i:Object in args)
    {
        trace(typeof(i) + ": " + i);
    }
}

something("hello", 4, new Sprite()); // string: hello
                                     // number: 4
                                     // object: [object Sprite]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文