Demo for Event code

Source: Internet
Author: User

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace EventDemo
{
Delegate void TemperatureChangedEventHandle (object sender, BoiledWaterEventArgs args );
Class Program
{
Static void Main (string [] args)
{
Heater ht = new Heater ();
Monitor mt = new Monitor ();
Ht. TemperatureChanged + = new TemperatureChangedEventHandle (Monitor. MonitorTemperature );
Ht. PowerOn ();
}
}
Class Heater
{
Public Heater ()
{
Type = "strong and fast ";
Area = "Made in China, Made by Trump ";
}
Private int _ temperature;
Public event TemperatureChangedEventHandle TemperatureChanged;
Public string Type
{
Get;
Private set;
}
Public string Area
{
Get;
Private set;
}
Public bool IsOnPower
{
Get;
Private set;
}
Public void PowerOff ()
{
IsOnPower = false;
String s = new string ('*', 50 );
Console. WriteLine ("My power supply is unplugged, now the temperature starts to drop, Type: {0}, Area: {1}", Type, Area );
Console. WriteLine (s );
System. Threading. Thread. Sleep (500 );
While (_ temperature> 0)
{
_ Temperature --;
System. Threading. Thread. Sleep (500 );
TemperatureChanged (this, new BoiledWaterEventArgs {Temperature = _ temperature, Type = this. Type, Area = this. Area });
}
}
Public void PowerOn ()
{
IsOnPower = true;
String s = new string ('*', 50 );
Console. WriteLine ("I started to boil my information, Type: {0}, Area: {1}", Type, Area );
System. Threading. Thread. Sleep (500 );
Console. WriteLine (s );
BoilWater ();
}
Private void BoilWater ()
{
While (IsOnPower & _ temperature <100)
{
_ Temperature ++;
System. Threading. Thread. Sleep (200 );
TemperatureChanged (this, new BoiledWaterEventArgs {Temperature = _ temperature, Type = this. Type, Area = this. Area });
}
}
}
Class Monitor
{
Public static void MonitorTemperature (object sender, BoiledWaterEventArgs args)
{
Heater ht = sender as Heater;
Int temperature = args. Temperature;
If (null = ht)
{
Console. WriteLine ("This is not a pitfall, sender is not a water heater", temperature );
Return;
}
If (temperature <30)
{
Console. WriteLine ("the water temperature is too low... goose bumps, temperature: {0}", temperature );
If (! Ht. IsOnPower)
{
Ht. PowerOn ();
}
}
Else if (temperature> = 30 & temperature <50)
{
Console. WriteLine ("water temperature is good, very good, temperature: {0}", temperature );
}
Else if (temperature >=50 & temperature <70)
{
Console. WriteLine ("a bit hot, tolerable, right when the sauna, temperature: {0}", temperature );
}
Else if (temperature> = 70)
{
Console. WriteLine ("You want to burn your brother, turn off the power supply, temperature: {0}", temperature );
If (ht. IsOnPower)
{
Ht. PowerOff ();
}
}
}
}
Class BoiledWaterEventArgs: EventArgs
{
Public int Temperature
{
Get;
Set;
}
Public string Type
{
Get;
Set;
}
Public string Area
{
Get;
Set;
}
}
}

 

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.