本文将actionscript3.0 (下称:as3) 与 C# 函数的声明及使用相比较
1.函数定义比较:
C#就很简单了,只有一种声明方式:访问权限关键字 初始化时间关键字 返回值 函数名(参数){};
public
static
void
getBook()
... {
//方法内容代码
}
// 带参数
public static void getBook( string str)
... {
//方法内容代码
}
// 带不定参数
public static void getBook( params object [] paras)
... {
//方法内容代码
}
... {
//方法内容代码
}
// 带参数
public static void getBook( string str)
... {
//方法内容代码
}
// 带不定参数
public static void getBook( params object [] paras)
... {
//方法内容代码
}
再来看看as3:
/**/
/*
ActionScript3.0 函数申明有两种方法,语句定义法与表达式定义法
*/
// 函数申明
function 函数名(参数:参数类型,....):返回值类型
... {
//函数内部语句
}
// 例子:
function getBook(a: int ,b: int ): int
... {
return a+b;
}
// 表达式定义
var 函数名:Function = function (参数:参数类型,.....):返回值类型
... {
//函数内部语句
}
// 例子:
var getBook:Function = function (a: int ,b: int ): int
... {
return a+b;
}
ActionScript3.0 函数申明有两种方法,语句定义法与表达式定义法
*/
// 函数申明
function 函数名(参数:参数类型,....):返回值类型
... {
//函数内部语句
}
// 例子:
function getBook(a: int ,b: int ): int
... {
return a+b;
}
// 表达式定义
var 函数名:Function = function (参数:参数类型,.....):返回值类型
... {
//函数内部语句
}
// 例子:
var getBook:Function = function (a: int ,b: int ): int
... {
return a+b;
}
由此可看出C#与as3函数定义的不同,在此处特外强调一下,函数参数定义的不定参数个数的区别
C#使用params 关键字来定义不定个数参数,as3用...来定义,举个例子:
C#:
//
为了便于看到,所以就用了winform程序
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace file1
... {
class Program
...{
static void Main(string[] args)
...{
getBooks("C#指南","ActionScript3.0 殿堂之路");
}
public static void getBooks(params object[] a)
...{
foreach (string j in a)
...{
Console.WriteLine(j);
}
}
}
}
// 此时您可以看到输入的是:C#指南 ActionScript3.0 殿堂之路
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace file1
... {
class Program
...{
static void Main(string[] args)
...{
getBooks("C#指南","ActionScript3.0 殿堂之路");
}
public static void getBooks(params object[] a)
...{
foreach (string j in a)
...{
Console.WriteLine(j);
}
}
}
}
// 此时您可以看到输入的是:C#指南 ActionScript3.0 殿堂之路
再来看看As3:
//
这里的paras可以是任何在as3中合法的名字,如:book,car......
function getBooks(...paras): void
... {
trace("参数长度"+paras.length);
for(var i in paras)
...{
trace(paras[i]);
}
}
getBooks( 1 ,[ 2 , 3 ], " String Type " );
/**/ /*
输出:
1
2,3
String Type
*/
function getBooks(...paras): void
... {
trace("参数长度"+paras.length);
for(var i in paras)
...{
trace(paras[i]);
}
}
getBooks( 1 ,[ 2 , 3 ], " String Type " );
/**/ /*
输出:
1
2,3
String Type
*/
再使用不定参数个数时,在定义不定参数的后面将不能再定义参数,前面是可以的,而且如果前面定义了参数
其类型一定要一致
如:
//
C#中你定义的是:
public class Mat
... {
public static void format(string str,params object[] a)
...{
foreach (string j in a)
...{
Console.WriteLine(j);
}
}
//错误
/**//*
public static void formatB(string str,params object[] a,int i)
{
foreach (string j in a)
{
Console.WriteLine(j);
}
}
*/
}
// 而使用时用:
Mat.format( 1 , 2 , 3 , 4 );将会报第一个参数类型错误
public class Mat
... {
public static void format(string str,params object[] a)
...{
foreach (string j in a)
...{
Console.WriteLine(j);
}
}
//错误
/**//*
public static void formatB(string str,params object[] a,int i)
{
foreach (string j in a)
{
Console.WriteLine(j);
}
}
*/
}
// 而使用时用:
Mat.format( 1 , 2 , 3 , 4 );将会报第一个参数类型错误
再来看看As3:
//
强调一下,这里逗号后的...(必须是三个,不能多也不能少)
function getBooks(firstPara:String,...paras): void ... {} // 正确
function getBooks(firstPara:String,...paras,str:String): void ... {} // 错误,...后不能再有参数
getBooks( " as3 " , " flash " ); // 正确
getBooks( 1 , " as3 " ); // 错误,第一个参数类型不正确
function getBooks(firstPara:String,...paras): void ... {} // 正确
function getBooks(firstPara:String,...paras,str:String): void ... {} // 错误,...后不能再有参数
getBooks( " as3 " , " flash " ); // 正确
getBooks( 1 , " as3 " ); // 错误,第一个参数类型不正确
由此可以看出两者的不同,由于本次不是专讲解As3函数的使用,所以对As3中函数使用的技巧将在下一节中讲
到.......