Как добавить событие, которое будет реагировать на изменения в обьекте? - C#
Формулировка задачи:
Здравствуйте, имеется код:
Подскажите, как добавить событие, которое будет реагировать на изменения в обьекте? (используя event и delegate).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Point
{
class Point
{
protected int x;
protected int y;
public Point()
{
x = 5;
y = 5;
}
public Point(int k, int g)
{
x = k;
y = g;
}
public Point(Point p)
{
x = p.x;
y = p.y;
}
public int getx()
{
return x;
}
public int gety()
{
return y;
}
public void setx(int t)
{
x = t;
}
public void sety(int t)
{
y = t;
}
public double distance(Point p)
{
return Math.Sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
public void plus(Point p)
{
x = x + p.x;
y = y + p.y;
}
public bool testconv(Point p)
{
if ((x == p.x) && (y == p.y)) return true;
else return false;
}
public void displayin()
{
Console.WriteLine(" enter point\n");
x = y = Convert.ToInt32(Console.ReadLine());
}
public virtual void displayout()
{
Console.WriteLine(string.Format("Point({0},{1})\n", x, y));
}
class PColor : Point
{
private string c;
public override void displayout()
{
Console.WriteLine("Color " + c);
}
public PColor()
{
c = "white";
}
public PColor(int x1, int y1, string c1) : base(x1, y1)
{
c = c1;
}
public PColor(PColor p) : base(p)
{
c = p.c;
}
}
static void Main(string[] args)
{
int x, y;
Point a = new Point();
a.displayout();
a.displayin();
Console.WriteLine("you entered: " + a.getx() + " " + a.gety());
a.displayout();
Console.WriteLine("copy constructor test: ");
Point b = new Point(a);
b.displayout();
Console.WriteLine("enter x,y:\n");
x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());
a.setx(x);
a.sety(y);
Console.WriteLine("setter test: ");
a.displayout();
Console.WriteLine("parameter constructor test: ");
Point c = new Point(x, y);
c.displayout();
Console.WriteLine("distance: " + c.distance(b));
if (b.testconv(c)) Console.WriteLine("they are equal\n");
else Console.WriteLine("they are not equal\n");
Console.WriteLine("adding points result: ");
a.plus(b);
a.displayout();
PColor f = new PColor();
f.displayout();
PColor f1 = new PColor(3, 3, "green");
f1.displayout();
PColor f2 = new PColor(f1);
f2.displayout();
Console.ReadKey();
}
}
}Решение задачи: «Как добавить событие, которое будет реагировать на изменения в обьекте?»
textual
Листинг программы
using System;
namespace ConsoleApplication65
{
class Program
{
class Point
{
protected int x;
protected int y;
public delegate void PointCoordsChangedHandler(Point sender,int oldx, int newx, int oldy, int newy);
public event PointCoordsChangedHandler CoordsChanged = delegate { };
public Point()
{
x = 5;
y = 5;
}
public Point(int k, int g)
{
x = k;
y = g;
}
public Point(Point p)
{
x = p.x;
y = p.y;
}
public int getx()
{
return x;
}
public int gety()
{
return y;
}
public void setx(int t)
{
int mem = x;
x = t;
CoordsChanged(this, mem, x, y, y);
}
public void sety(int t)
{
int mem = y;
y = t;
CoordsChanged(this, x, x, mem, y);
}
public double distance(Point p)
{
return Math.Sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
public void plus(Point p)
{
int memx = x, memy = y;
x = x + p.x;
y = y + p.y;
CoordsChanged(this, memx, x, memy, y);
}
public bool testconv(Point p)
{
if ((x == p.x) && (y == p.y)) return true;
else return false;
}
public void displayin()
{
Console.WriteLine(" enter point\n");
x = y = Convert.ToInt32(Console.ReadLine());
}
public virtual void displayout()
{
Console.WriteLine(string.Format("Point({0},{1})\n", x, y));
}
}
class PColor : Point
{
private string c;
public override void displayout()
{
Console.WriteLine("Color " + c);
}
public PColor()
{
c = "white";
}
public PColor(int x1, int y1, string c1) : base(x1, y1)
{
c = c1;
}
public PColor(PColor p) : base(p)
{
c = p.c;
}
}
static void Main(string[] args)
{
int x, y;
Point a = new Point();
a.CoordsChanged+=CoordsChangedEventHandler;
a.displayout();
a.displayin();
Console.WriteLine("you entered: " + a.getx() + " " + a.gety());
a.displayout();
Console.WriteLine("copy constructor test: ");
Point b = new Point(a);
b.CoordsChanged += CoordsChangedEventHandler;
b.displayout();
Console.WriteLine("enter x,y:\n");
x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());
a.setx(x);
a.sety(y);
Console.WriteLine("setter test: ");
a.displayout();
Console.WriteLine("parameter constructor test: ");
Point c = new Point(x, y);
c.CoordsChanged += CoordsChangedEventHandler;
c.displayout();
Console.WriteLine("distance: " + c.distance(b));
if (b.testconv(c)) Console.WriteLine("they are equal\n");
else Console.WriteLine("they are not equal\n");
Console.WriteLine("adding points result: ");
a.plus(b);
a.displayout();
PColor f = new PColor();
f.displayout();
PColor f1 = new PColor(3, 3, "green");
f1.displayout();
PColor f2 = new PColor(f1);
f2.displayout();
Console.ReadKey();
}
static void CoordsChangedEventHandler(Point sender, int oldx, int newx,int oldy, int newy) {
Console.WriteLine(String.Format(@"Координаты точки измерились, были ({0},{1}), стали ({2},{3})",oldx,oldy,newx,newy));
}
}
}