假设我有一个名为Frog的类,它看起来像:
public class Frog
{
public int Location { get; set; }
public int JumpCount { get; set; }
public void OnJump()
{
JumpCount++;
}
}
我需要2件事的帮助:
最佳答案
public event EventHandler Jump;
public void OnJump()
{
EventHandler handler = Jump;
if (null != handler) handler(this, EventArgs.Empty);
}
然后
Frog frog = new Frog();
frog.Jump += new EventHandler(yourMethod);
private void yourMethod(object s, EventArgs e)
{
Console.WriteLine("Frog has Jumped!");
}
关于c# - 如何将事件添加到类(class),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/85137/