Advertisement
renurtt

Untitled

Sep 23rd, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace events
  5. {
  6.     public class AirCraftParameters : EventArgs
  7.     {
  8.         public  AirCraftParameters(int alt, int speed)
  9.         {
  10.             Altitude = alt;
  11.             Speed = speed;
  12.         }
  13.  
  14.         public readonly int Altitude;
  15.         public readonly int Speed;
  16.     }
  17.     public class Ground
  18.     {
  19.         public static void GroundHandler(object obj, AirCraftParameters e)
  20.         {
  21.             Console.WriteLine(obj.ToString()+", it's the Ground, your parameters: alt="+e.Altitude+", speed="+e.Speed);
  22.         }
  23.     }
  24.  
  25.     public class B737
  26.     {
  27.         public void b737Handler(object obj, AirCraftParameters e)
  28.         {
  29.             Console.WriteLine(obj.ToString() + ", it's Boeing737, all OK!");
  30.         }
  31.     }
  32.  
  33.     public class A300
  34.     {
  35.         public void a300Handler(object obj, AirCraftParameters e)
  36.         {
  37.             Console.WriteLine(obj.ToString() + ", it's AirbusA300, everything's perfect!");
  38.         }
  39.     }
  40.     //public delegate void AircraftEventHandler(object sender, AirCraftParameters e);
  41.     public class B787
  42.     {
  43.         public override string ToString()
  44.         {
  45.             return "Boeing787";
  46.         }
  47.  
  48.         public event EventHandler<AirCraftParameters> TheSituationEvent;
  49.  
  50.         public void CheckTheSituation()
  51.         {
  52.             int times = 1;
  53.             Console.WriteLine("Checking...");
  54.             for (int i = 0; i < times; i++)
  55.             {
  56.                 TheSituationEvent?.Invoke(this,new AirCraftParameters(35000, 260));
  57.                 System.Threading.Thread.Sleep(1000);
  58.             }
  59.  
  60.             Console.WriteLine("Connection successfully disrupted");
  61.         }
  62.     }
  63.     internal class Program
  64.     {
  65.         public static void Main(string[] args)
  66.         {
  67.             B787 b787 = new B787();
  68.             B737 b737 = new B737();
  69.             A300 a300 = new A300();
  70.  
  71.             b787.TheSituationEvent += Ground.GroundHandler;
  72.             b787.TheSituationEvent += b737.b737Handler;
  73.             b787.TheSituationEvent += a300.a300Handler;
  74.  
  75.             b787.CheckTheSituation();
  76.             b787.TheSituationEvent -= Ground.GroundHandler;
  77.             Console.WriteLine("*** communication with the earth is lost");
  78.             b787.CheckTheSituation();
  79.             b787.TheSituationEvent -= b737.b737Handler;
  80.             b787.TheSituationEvent -= a300.a300Handler;
  81.             Console.WriteLine("*** communication with other aircrafts is lost");
  82.             b787.CheckTheSituation();
  83.            
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement