Chapter 5: The agent agent is implemented by a pointer function such as a language such as C , a different function pointer, an agent is an object-oriented, secure type. Acting a reference type of a public base class (system), the method is pressed into an agent, and the instance method is called the instance of the instance of the instance, or the method of the instance, and the static method is called the composition of the class. Entity or class method. The powerful function of the agent is that it can automatically match the method, regardless of its type.
Write a agent to include three steps:
Write agency, instantiate, call.
Agent's statement uses the following syntax: delegate void SimpleDelegate ();
Instantiate a proxy class test {static void f () {system.console.writeline ("Hello World");} static void main () {SimpleDelegate D = New SimpleDelegate (f); // Press the method into D () ; // passed the agent; f (); // does not pass the agent;}}
Finally let us call her
Void Multicall (INT I = 0; i We can see that the call to the method is done by the agent. When the call is called, it is not necessary to know the type called her. Agent is in my opinion, which is an object, she doesn't call this method directly, but call her through a middleman. Let's take a detailed introduction to the powerful function of the agent: First, we implemented such a function, consider how to use the member function of the subclass with the object to the base class. It is still a point in love, but it is fully realized in such a function in C #, using a separate agent we can complete this feature. The following code comes from the Timothy A. Vanover article. Namespace delegatescs {using system; public class Wisdom // class containing agents {public delegate string GiveAdvice (); public string OfferAdvice (GiveAdvice Words) {return Words ();}} public class Parent // base class {public virtual string Advice () {return ( " Listen to REason ");} ~ parent () {}} public class Dad: Parent // Sub-class {public DAD () {Return (" Listen to Your Mom ");} ~ DAD () {}} PUBLIC CLASS MOM: PARENT // Subclass {Public Overs () {} PUBLIC OVERRIDE STRING Advice () {Return ("Listen to Your Dad");} ~ Mom () {}} public class daugging / Do not inherit the class {public daughter () {Return ("i know all there is to life");} ~ daughter ()}}} public class Test {public static string CallAdvice (Parent p) // use the base class {Wisdom parents = new Wisdom (); Wisdom.GiveAdvice TeenageGirls = new Wisdom.GiveAdvice (p.Advice); // Advice methods to delegate trust to TeenageGirls Object Return (TEENTS.OFFERADVICE (TEENAGEGIRLS));} public static void main () {DAD D = New DAD (); MOM m = new mom (); Daughter g = new daughter (); // The following two is derived Class console.writeline (Calladvice (m)); // below is not derived from the class, if the call will be wrong. //Console.writeline (colladvice);}}}