C#接口事件 测试

mac2022-06-30  22

using System; using System.Collections.Generic; using System.Text; namespace ImplementInterfaceEvents{ interface IDrawingObject { event EventHandler ShapeChanged; } public class MyEventArgs : EventArgs { private string dispalyMsg; public string DispalyMsg { get { return dispalyMsg; } } public MyEventArgs() : this ( string .Empty) { } public MyEventArgs( string msg) { this .dispalyMsg = msg; } } public class Shape : IDrawingObject { public event EventHandler ShapeChanged; public void ChangeShape() { Console.WriteLine( " Do something here before the event… " ); OnShapeChanged( new MyEventArgs( " hello " )); Console.WriteLine( " or do something here after the event . " ); } protected virtual void OnShapeChanged(MyEventArgs e) { if (ShapeChanged != null ) { ShapeChanged( this , e); } } } public class TestDemo { static void Main( string [] args) { Shape s = new Shape(); s.ShapeChanged += new EventHandler(s_ShapeChanged); s.ChangeShape(); Console.ReadLine(); } static void s_ShapeChanged( object sender, EventArgs e) { Console.WriteLine(((MyEventArgs)e).DispalyMsg); Console.WriteLine( " s_ShapeChanged " ); } }}

转载于:https://www.cnblogs.com/fightLonely/archive/2011/02/17/1957064.html

最新回复(0)