using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BallEvent
{
class BallEventArgs:EventArgs
{
public int Trajectory { get; private set; }
public int Distance { get; private set; }
public BallEventArgs(int trajectory, int distance)
{
this.Trajectory = trajectory;
this.Distance = distance;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/////////////////////////////////////////////////////////////////////
namespace BallEvent
{
class Ball
{
public event EventHandler BallInPlay;
public void OnBallInPlay(BallEventArgs e)
{
EventHandler ballInPlay = BallInPlay;
if (ballInPlay != null)
ballInPlay(this, e);
}
}
}
////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BallEvent
{
class Pitcher
{
public Pitcher(Ball ball)
{
ball.BallInPlay += new EventHandler(ball_BallInPlay);//тут нажимаем 2 раза TAB
}
void ball_BallInPlay(object sender, EventArgs e)
{
if (e is BallEventArgs)
{
BallEventArgs ballEventArgs = e as BallEventArgs;
if ((ballEventArgs.Distance < 95) && (ballEventArgs.Trajectory < 60))
CatchBall();
else
CoverFirstBase();
}
}
private void CatchBall()
{
Console.WriteLine("Pitcher: I caught the ball");
}
private void CoverFirstBase()
{
Console.WriteLine("Pitcher: I covered first base");
}
}
}
/////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BallEvent
{
class Fan
{
public Fan(Ball ball)
{
ball.BallInPlay += new EventHandler(ball_BallInPlay);
}
void ball_BallInPlay(object sender, EventArgs e)
{
if (e is BallEventArgs)
{
BallEventArgs ballEventArgs = e as BallEventArgs;
if (ballEventArgs.Distance > 400 && ballEventArgs.Trajectory > 30)
Console.WriteLine("Fan: Home run! I'm going for the ball ! ");
else
Console.WriteLine("Fan: Woo-hoo ! Yeah ! ");
}
}
}
}
/////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BallEvent
{
public partial class Form1 : Form
{
Ball ball = new Ball();
Pitcher pitcher;
Fan fan;
public Form1()
{
InitializeComponent();
pitcher = new Pitcher(ball);
fan = new Fan(ball);
}
private void button1_Click(object sender, EventArgs e)
{
BallEventArgs ballEventArgs = new BallEventArgs((int)numericUpDown1.Value, (int)numericUpDown2.Value);
ball.OnBallInPlay(ballEventArgs);
}
}
}