Promoting this concept is used in C # / Java, a bit like a function pointer in C , but is more advanced than it. Here there is MSDN's document on the Delegate class: http://msdn.microsoft.com/library/chs /Default.asp?url=/library/chs/cpref/html/frlrfsystemdeLegateClassctOrTopic.asp
How is ActionScript2 implement it? I remember that Grant Skinner wrote a EventProxy class very early, then Mike Chambers, and finally MM officially put MX.UTILS.DELEGATE class of Mike now Flash 2004 Updater2. Personally think that this class is not very good, and many places cannot be made. Maybe this is related to the language. I was also studying this in this year a month, see if there is a way to write a Delegate class to do some more convenient use, and then encounter a technical problem. It is ActionScript2 to achieve reflection, this is still not successful for a long time. If you can implement it in the previous ActionScript Prototype syntax.
Take some research now. Mike's Tutorial: http://www.macromedia.com dd/EVENTPROXY.HTMLRICN Translation: http://www.riacon.com/web/showArticle.asp? id = 72
Turning before I wrote: The entrustment is not only an event agent. Mike only said to Delegate's use. I will raise another simple example, then I will add some tips to everyone.
Pre {Font-Family: "Courier New", Courier, Arial; font-size: 12px;}
. galan {color: # 000000;
. readyword {color: # 993300;
.IDENTIFIER {color: # 000087;
.properties {color: # 000087;
{Color: # 000087;
.LINECMMMENT, .BLOCKCOMMENT {Color: # 808080;
. String {color: # 0000ff;}
// ******************************************************** ******
@FileName Democlass.as
@Package
@Description demonstration class, there are two static methods
@Author AOL
@Email jeremy1982@21cn.com
@Create 2004.07.29
@Lastchange 2004.07.29
@History
// ******************************************************** ******
Class Democlass
{
Public Static Function Fun1 (Arg: String): String
{
Var str: String = "The result of the first function" arg;
Return Str;
}
Public Static Function Fun2 (Arg: String): String
{
Var str: String = "The result of the second function" arg;
Return Str;
}
}
//*.fla
Import mx.utils.delegate;
Var delegateArray: array = [
Delegate.create (Null, Democlass.fun1), Delegate.create (Null, Democlass.fun2)
];
Function Display (f: function, arg: string)
{
Trace (f (arg));
}
For (VAR i: Number = 0; i
Length; i )
{
TRACE
"Start with DelegateArray [" i
"]");
Display (DelegateArray [i],
"Parameter 1");
Display (DelegateArray [i],
"Parameter 2");
Display (DelegateArray [i],
"Parameter Three");
}
The Output window returns:
Start with the result of the result parameter of the first function of DelegateArray [0] The result parameter of the first function of the first function The result parameter three start with the result parameters of the second function of DelegateArray [1] The result of the result of the result parameter two second function results parameter three
Of course, we can also call the object creatededegegate (o: object) method by constructing the object of the Delegate class.
Import mx.utils.delegate;
Var delegateArray: array = [
New delegate (Democlass.fun1),
New delegate (Democlass.fun2)
];
Function Display (D: DELEGATE, ARG: STRING)
{
TRACE (D.CreateDelegate () ());
}
For (VAR i: Number = 0; i
Length; i )
{
TRACE
"Start with DelegateArray [" i
"]");
Display (DelegateArray [i],
"Parameter 1");
Display (DelegateArray [i],
"Parameter 2");
Display (DelegateArray [i],
"Parameter Three");
}
The above call is a static method of the class, the DELEGATE object can reference the non-static method:
// ******************************************************** ******
@FileName InstanceDemo.as
@Package
@Description demonstration class, there are two instance methods
@Author AOL
@Email jeremy1982@21cn.com
@Create 2004.09.20
@Lastchange 2004.09.20
@History
// ******************************************************** ******
Class InstanceDemo
{
Public Function Fun1 (Arg: String): String
{
Var str: String = "The result of the first function" arg;
Return Str;
}
Public Function Fun2 (arg: string): String
{
Var str: String = "The result of the second function" arg;
Return Str;
}
}
//instancedemo1.fla
Import mx.utils.delegate;
Var A: instancedemo = new instancedemo (); var delegateArray: array = [
Delegate.create (A, A.Fun1),
Delegate.create (a, a.fun2)
];
Function Display (f: function, arg: string)
{
Trace (f (arg));
}
For (VAR i: Number = 0; i
Length; i )
{
TRACE
"Start with DelegateArray [" i
"]");
Display (DelegateArray [i],
"Parameter 1");
Display (DelegateArray [i],
"Parameter 2");
Display (DelegateArray [i],
"Parameter Three");
}
//instancedemo2.fla
Import
mx.utils.delegate;
Var delegateArray:
Array = [
New delegate
New instancedemo (). fun1),
New delegate
New instancedemo (). FUN2)
];
Function Display (D: DELEGATE, ARG:
String)
{
TRACE (D.CreateDelegate () ());
}
FOR
VAR i:
Number = 0; i
Length; i )
{
TRACE
"Start with DelegateArray [" i
"]");
Display (DelegateArray [i],
"Parameter 1");
Display (DelegateArray [i],
"Parameter 2");
Display (DelegateArray [i],
"Parameter Three");
}