[Big talk. Net] to commemorate the ghost program in 918-Delegation

Source: Internet
Author: User

When I went to work in the afternoon, I heard a high volume of air defense outside the window. Once again, I realized that today is 9.18 national shame. History cannot be forgotten ......
I am reading the msdn2005 chapter, which is the commissioned chapter. I have read the example of the average price of the books in the computing library. After understanding it, I want to try it myself. In the past, I also saw an example of a ghost program in another blog. At that time, I did not understand it carefully. Today, I have basically understood the meaning of the delegation, so I came up with an example of a ghost program as per my own understanding. By the way, I will remember 9.18.

Assuming that the devils swept away in the anti-Japanese base area, my brave people fought back and used the tactics of the thunder war. Assume that the scene of the movie is as follows: Zhang cazi, a guerrilla team, has laid a mine in Zhang's yard, and several devils have entered the yard. When Zhang Caizi, in the distance, has a string, and the devils will be cool.

We need three roles (classes ):
1. Yard

// Yard
Class Yard
{
// Yard name
Public   String Yardname;

// Mines
Public Bomb bombinyard;

// Devils
Public   Int Japanesenumber;

Public Yard ( String Name)
{
Yardname=Name;
Console. writeline ("Yard: {0}", Yardname );
}

// The devil is in.
Public   Void Japanesein ( Int Number)
{
Japanesenumber=Number;
Console. writeline ("Yard enters {0} devils", Number );
}
}

2. Mines

// Mines
Class Bomb
{
// Mine Explosion
Public   Void Dobomb ()
{
Console. writeline ("The devil is killed!");
}
}

3. Zhang cazi (main character)

// Zhang ga
Class Zhangga
{
Public Zhangga ()
{
Console. writeline ("Zhang cazi, a small guerrilla contestant, debuted");
}

// Placement of Mines
Public   Void Setbomb (yard Yad)
{
Console. writeline ("{0} has been deprecated", Yad. yardname );
Yad. bombinyard= NewBomb ();
}

 

// String pulling

Public   Void Pullfireline (fireline fire)

{

Console. writeline ("It's a string.");

Fire ();

}

}

4. The next step is another important role that cannot be ignored.

// Declare a mine fuse
Public   Delegate   Void Fireline ();

After several roles have been set up, the show starts.

// Movie
Class Movie
{
Static   Void Main ()
{
Console. writeline ( " Movie started " );

// Zhang cazi, the main character, debuted
Zhangga ZG =   New Zhangga ();

// Zhang jiayuan
Yard yardzhang =   New Yard ( " Zhang jiayuan " );

// Garzi has buried mines.
ZG. setbomb (yardzhang );

// Three devils entered zhangjiayuan.
Yardzhang. japanesein ( 3 );

// Dr. Zhang pulled the string.
ZG. pullfireline ( New Fireline (yardzhang. bombinyard. dobomb ));
// The previous sentence in vs2005 can be replaced by the following sentence:
// ZG. pullfireline (yardzhang. bombinyard. dobomb );

Console. writeline ( " The movie is over. " );

Console. Readline ();
}  
}

Execution result:

You can read the aboveProgram? YesCodeCopy to vs2003 and try it. If it is the vs2005 environment, in the main function of the movie class, the string pulling method can also be changed to the simple writing method in the Code, with the same effect. 

Let's talk about the delegation you understand. The delegate is to use the delegate method object as a parameter and connect the methods of one class to a method of another class to accomplish a complete task, complete the steps related to yourself in both classes. Method To be connected. The parameter type must be the same as that of the delegate method object.

In the preceding example, the fuse is used as a delegate method object (the delegate method here is a bit special and there is no parameter set for the delegate method object. If so, the parameters of the mine explosion method should be the same.) the Mine Explosion Method is connected to the string pulling method. The actions of the mine are of course just string pulling, and then wait for the mine to respond. As long as they are not fake and shoddy mines, mines themselves know how to explosion. As a result, the mine burst with a string. The great little anti-Japanese hero, brave guerrilla fighters, and strong revolutionary fighters are possessed by the gods at this moment ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.