/ First, referring to a statement, it defines an event signal that wakes up a function.
Public Delegate Void ScorechangeEventHandler (Int Newscore, Ref Bool Cancel)
;
/ / Define a class that generates events
Public Class Game
{
// Note that this is used here.
Public Event ScorechangeEventHandler Scorechange;
int score;
// score properties
Public int score
{
get
{
Return score;
}
set
{
IF (Score! = Value)
{
Bool Cancel = False;
Scorechange (Value, Ref Cancel);
IF (! ca Zancel)
Score = value;
}
}
}
// Treat the class
Public Class Referee
{
Public Referee (Game Game)
{
// The referee is responsible for adjusting the score changes in the competition
Game.scorechange = new scorechangeeenthandler (game_scorechange);
}
/ / Note What is the function here and the signal of ScorechangeEventHandler
Private void game_scorechange (int newscore, ref boce)
{
NewsCore <100)
System.console.writeline ("Good Score");
Else
{
CANCEL = True;
System.console.writeline ("No Score Can Be That High!");
}
}
}
// Main function class, used to test the above characteristics
Public Class Gametest
{
Public static void main ()
{
Game game = new game ();
Referee Referee = New Referee (Game);
Game.score = 70;
Game.score = 110;
}
}
This code first defines a delegate type, followed by creating a Class Game, defining the delegate event, by calling the attributes, calling the event defined in delegate, the Refacte Class is mainly initialized event processing, completing the class game RefeRee initialization, and put the Delegate event, prepare for the GAME class.