1.16.定义方法参数

1.16.1.问题
我想定义一个方法,其参数有默认值或null值,以便调用方法时不必每次都进行传值。
1.16.2.解决办法
在方法申明时直接对方法参数进行赋值,赋予默认值或null值。
1.16.3.讨论
要想为方法定义一个或多个可选参数,最简单的办法就是为参数对象设置为默认值或null。

ActionScript基本类型String, Number, int, 和Boolean不能设置为null值,不过可以设置一个默认值,例如:
+展开
-ActionScript
public function optionalArgumentFunction(value:Object,string:String, count:int = 0,otherValue:Object = null):void{
if (count != 0) {
/*if the count is not the default value handle the value the call passes in*/
}
if (otherValue != null ) {
/* if the otherValue is not null handle the value the call passes in */
}
}

还有一个策略就是在变量名前使用...标记定义可选参数以及不定数量的参数。该变量将包含一个参数数组,可被循环遍历处理。
+展开
-ActionScript
public function unlimitedArgumentsFunction(...arguments):void {
for each (var arg:Object in arguments) {
/* process each argument */
}
}

加支付宝好友偷能量挖...


评论(0)网络
阅读(132)喜欢(0)flash/flex/fcs/AIR