Как добавить событие, которое будет реагировать на изменения в обьекте? - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте, имеется код:
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Point
  7. {
  8. class Point
  9. {
  10. protected int x;
  11. protected int y;
  12. public Point()
  13. {
  14. x = 5;
  15. y = 5;
  16. }
  17. public Point(int k, int g)
  18. {
  19. x = k;
  20. y = g;
  21. }
  22. public Point(Point p)
  23. {
  24. x = p.x;
  25. y = p.y;
  26. }
  27. public int getx()
  28. {
  29. return x;
  30. }
  31. public int gety()
  32. {
  33. return y;
  34. }
  35. public void setx(int t)
  36. {
  37. x = t;
  38. }
  39. public void sety(int t)
  40. {
  41. y = t;
  42. }
  43. public double distance(Point p)
  44. {
  45. return Math.Sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
  46. }
  47. public void plus(Point p)
  48. {
  49. x = x + p.x;
  50. y = y + p.y;
  51. }
  52. public bool testconv(Point p)
  53. {
  54. if ((x == p.x) && (y == p.y)) return true;
  55. else return false;
  56. }
  57. public void displayin()
  58. {
  59. Console.WriteLine(" enter point\n");
  60. x = y = Convert.ToInt32(Console.ReadLine());
  61. }
  62. public virtual void displayout()
  63. {
  64. Console.WriteLine(string.Format("Point({0},{1})\n", x, y));
  65. }
  66. class PColor : Point
  67. {
  68. private string c;
  69. public override void displayout()
  70. {
  71. Console.WriteLine("Color " + c);
  72. }
  73. public PColor()
  74. {
  75. c = "white";
  76. }
  77. public PColor(int x1, int y1, string c1) : base(x1, y1)
  78. {
  79. c = c1;
  80. }
  81. public PColor(PColor p) : base(p)
  82. {
  83. c = p.c;
  84. }
  85. }
  86. static void Main(string[] args)
  87. {
  88. int x, y;
  89. Point a = new Point();
  90. a.displayout();
  91. a.displayin();
  92. Console.WriteLine("you entered: " + a.getx() + " " + a.gety());
  93. a.displayout();
  94. Console.WriteLine("copy constructor test: ");
  95. Point b = new Point(a);
  96. b.displayout();
  97. Console.WriteLine("enter x,y:\n");
  98. x = Convert.ToInt32(Console.ReadLine());
  99. y = Convert.ToInt32(Console.ReadLine());
  100. a.setx(x);
  101. a.sety(y);
  102. Console.WriteLine("setter test: ");
  103. a.displayout();
  104. Console.WriteLine("parameter constructor test: ");
  105. Point c = new Point(x, y);
  106. c.displayout();
  107. Console.WriteLine("distance: " + c.distance(b));
  108. if (b.testconv(c)) Console.WriteLine("they are equal\n");
  109. else Console.WriteLine("they are not equal\n");
  110. Console.WriteLine("adding points result: ");
  111. a.plus(b);
  112. a.displayout();
  113. PColor f = new PColor();
  114. f.displayout();
  115. PColor f1 = new PColor(3, 3, "green");
  116. f1.displayout();
  117. PColor f2 = new PColor(f1);
  118. f2.displayout();
  119. Console.ReadKey();
  120. }
  121. }
  122. }
Подскажите, как добавить событие, которое будет реагировать на изменения в обьекте? (используя event и delegate).

Решение задачи: «Как добавить событие, которое будет реагировать на изменения в обьекте?»

textual
Листинг программы
  1. using System;
  2.  
  3. namespace ConsoleApplication65
  4. {
  5.     class Program
  6.     {
  7.         class Point
  8.         {
  9.             protected int x;
  10.             protected int y;
  11.             public delegate void PointCoordsChangedHandler(Point sender,int oldx, int newx, int oldy, int newy);
  12.             public event PointCoordsChangedHandler CoordsChanged = delegate { };
  13.             public Point()
  14.             {
  15.                 x = 5;
  16.                 y = 5;
  17.             }
  18.  
  19.             public Point(int k, int g)
  20.             {
  21.                 x = k;
  22.                 y = g;
  23.             }
  24.  
  25.             public Point(Point p)
  26.             {
  27.                 x = p.x;
  28.                 y = p.y;
  29.             }
  30.  
  31.             public int getx()
  32.             {
  33.                 return x;
  34.             }
  35.  
  36.             public int gety()
  37.             {
  38.                 return y;
  39.             }
  40.  
  41.             public void setx(int t)
  42.             {
  43.                 int mem = x;
  44.                 x = t;
  45.                 CoordsChanged(this, mem, x, y, y);
  46.             }
  47.  
  48.             public void sety(int t)
  49.             {
  50.                 int mem = y;
  51.                 y = t;
  52.                 CoordsChanged(this, x, x, mem, y);
  53.             }
  54.  
  55.             public double distance(Point p)
  56.             {
  57.                 return Math.Sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
  58.             }
  59.  
  60.             public void plus(Point p)
  61.             {
  62.                 int memx = x, memy = y;
  63.                 x = x + p.x;
  64.                 y = y + p.y;
  65.                 CoordsChanged(this, memx, x, memy, y);
  66.             }
  67.  
  68.             public bool testconv(Point p)
  69.             {
  70.                 if ((x == p.x) && (y == p.y)) return true;
  71.                 else return false;
  72.             }
  73.  
  74.             public void displayin()
  75.             {
  76.                 Console.WriteLine(" enter point\n");
  77.                 x = y = Convert.ToInt32(Console.ReadLine());
  78.             }
  79.  
  80.             public virtual void displayout()
  81.             {
  82.                 Console.WriteLine(string.Format("Point({0},{1})\n", x, y));
  83.             }
  84.             }
  85.             class PColor : Point
  86.             {
  87.                 private string c;
  88.  
  89.                 public override void displayout()
  90.                 {
  91.                     Console.WriteLine("Color " + c);
  92.                 }
  93.  
  94.                 public PColor()
  95.                 {
  96.                     c = "white";
  97.                 }
  98.  
  99.                 public PColor(int x1, int y1, string c1) : base(x1, y1)
  100.                 {
  101.                     c = c1;
  102.                 }
  103.  
  104.                 public PColor(PColor p) : base(p)
  105.                 {
  106.                     c = p.c;
  107.                 }
  108.             }
  109.        
  110.         static void Main(string[] args)
  111.         {
  112.             int x, y;
  113.             Point a = new Point();
  114.             a.CoordsChanged+=CoordsChangedEventHandler;
  115.             a.displayout();
  116.             a.displayin();
  117.             Console.WriteLine("you entered: " + a.getx() + " " + a.gety());
  118.             a.displayout();
  119.             Console.WriteLine("copy constructor test: ");
  120.             Point b = new Point(a);
  121.             b.CoordsChanged += CoordsChangedEventHandler;
  122.             b.displayout();
  123.             Console.WriteLine("enter x,y:\n");
  124.             x = Convert.ToInt32(Console.ReadLine());
  125.             y = Convert.ToInt32(Console.ReadLine());
  126.             a.setx(x);
  127.             a.sety(y);
  128.             Console.WriteLine("setter test: ");
  129.             a.displayout();
  130.             Console.WriteLine("parameter constructor test: ");
  131.             Point c = new Point(x, y);
  132.             c.CoordsChanged += CoordsChangedEventHandler;
  133.             c.displayout();
  134.             Console.WriteLine("distance: " + c.distance(b));
  135.             if (b.testconv(c)) Console.WriteLine("they are equal\n");
  136.             else Console.WriteLine("they are not equal\n");
  137.             Console.WriteLine("adding points result: ");
  138.             a.plus(b);
  139.             a.displayout();
  140.  
  141.             PColor f = new PColor();
  142.             f.displayout();
  143.  
  144.             PColor f1 = new PColor(3, 3, "green");
  145.             f1.displayout();
  146.  
  147.             PColor f2 = new PColor(f1);
  148.             f2.displayout();
  149.  
  150.             Console.ReadKey();
  151.         }
  152.         static void CoordsChangedEventHandler(Point sender, int oldx, int newx,int oldy, int newy) {
  153.             Console.WriteLine(String.Format(@"Координаты точки измерились, были ({0},{1}), стали ({2},{3})",oldx,oldy,newx,newy));
  154.         }
  155.     }
  156. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.25 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы