1.函數
函數語句定義法:
function testAdd(a:int, b:int):int{return a+b;}
函數運算式定義法:
var test:Function = function(a:int, b:int):int{return a+b;}
1.1函數預設參數
function test(a:int = 3,b:int=2,c:int=1):void
{
trace(a+b+c,a,b,c);
}
test();//6,3,2,1
test(9);//12,9,2,1
test(2,9)//12,2,9,1
2.靜態屬性和方法
static var 屬性:屬性類型;
public static var 屬性:屬性類型 = 值;
static const 屬性:屬性類型 = 值; (常數就要先付予值)
static function 方法(參數):返回值類型{}
public static function 方法(參數):返回值類型{}
透過實例來存取靜態成員會出錯
var foo:StaticSample = new StaticSample(); // new了foo實例
trace(foo.CLASSNAME);
// 在StaticSample類別中有宣告一個靜態成員CLASSNAME
foo.hello();
//方法也是一樣
3.構造函數
函數語句定義法:
function testAdd(a:int, b:int):int{return a+b;}
函數運算式定義法:
var test:Function = function(a:int, b:int):int{return a+b;}
1.1函數預設參數
function test(a:int = 3,b:int=2,c:int=1):void
{
trace(a+b+c,a,b,c);
}
test();//6,3,2,1
test(9);//12,9,2,1
test(2,9)//12,2,9,1
2.靜態屬性和方法
static var 屬性:屬性類型;
public static var 屬性:屬性類型 = 值;
static const 屬性:屬性類型 = 值; (常數就要先付予值)
static function 方法(參數):返回值類型{}
public static function 方法(參數):返回值類型{}
透過實例來存取靜態成員會出錯
var foo:StaticSample = new StaticSample(); // new了foo實例
trace(foo.CLASSNAME);
// 在StaticSample類別中有宣告一個靜態成員CLASSNAME
foo.hello();
//方法也是一樣
3.構造函數
package
{
import flash.display.Sprite;
public class SampleConstructor extends Sprite
{
public function SampleConstructor()
{
var foo:Foo = new Foo();
trace(foo);
//輸出:[object Foo]
var bar:Bar = new Bar("ActionScript3.0");
//輸出Bar's counstructor!
//init executed!
//可以看出Bar一建立時,就執行了構造韓數中的語句
trace(bar);
//輸出:[object Bar]
trace(bar.isOK);
//輸出:ture
//bar.isOK屬性在構造函數中成功初始化成true
trace(bar.hello);
//輸出:ActionScript3.0
}
}
}
class Foo()
{
}
class Bar
{
public var isOK:Boolean;
public var hello:String;
public function Bar(hs:String)
{
trace("Bar's counstructor!");
isOK = true;
hello = hs;
init();
}
function init():void
{
trace("init executed!");
}
}
4.方法重載
package
{
import flash.display.Sprite;
public class SampleOverload extends Sprite
{
public function SampleOverload():void
{
overload();
overload(3);
overload("www.yahoo.com");
overload(12,"123",new Object());
}
}
private function overload(...args):*
{
//(...args)寫法是說args變成一個陣列,每次值型丟進去的值
//都匯存進陣列中,在運用函式去分析
//:* 這意思是,丟進來怎樣類型的值,此函數經過處理後一樣return相同類型的值
if(args.length == 0)return reportDefault();
if(args.length == 1)
{
if(typeof(args[0])=="number")
{
return reportNumber(args[0]);
}else{
return reportErr(args[0]);
}
}
if(args.length >1)return reportArray(args);
}
private function reportDefault():Boolean
{
trace("we got nothing");
return false;
}
private function reportNumber(num:Number):Number
{
trace("we got number:"+num);
return num;
}
private function reportErr(obj:*):*
{
trace("we don't understand this object:"+obj);
return obj;
}
private function reportArray(ary:Array):Array
{
trace("we got an array :"+ary);
return ary;
}
}
留言