Elo*_*noa 4 parameters types arguments actionscript-3
任何人都可以告诉我如何在ActionScript3.0中创建一个类似下面的函数?
function test(one:int){ trace(one);}
function test(many:Vector<int>){
for each(var one:int in many){ test(one); }
}
Run Code Online (Sandbox Code Playgroud)
您可以使用星号和is关键字:
function test(param:*):void
{
if(param is int)
{
// Do stuff with single int.
trace(param);
}
else if(param is Vector.<int>)
{
// Vector iteration stuff.
for each(var i:int in param)
{
test(i);
}
}
else
{
// May want to notify developers if they use the wrong types.
throw new ArgumentError("test() only accepts types int or Vector.<int>.");
}
}
Run Code Online (Sandbox Code Playgroud)
对于具有两个明确标记的方法,这很少是一种很好的方法,因为如果没有特定的类型要求,很难说这些方法的意图是什么.
我建议一套更清晰的方法,恰当地命名,例如
function testOne(param:int):void
function testMany(param:Vector.<int>):void
Run Code Online (Sandbox Code Playgroud)
在这种特殊情况下可能有用的东西就是...rest争论.这样,您可以允许一个或多个整数,并为其他人(以及稍后您自己)提供更多可读性,以了解该方法的作用.
| 归档时间: |
|
| 查看次数: |
1453 次 |
| 最近记录: |