事件申明、订阅和处理
1 using System; 2 using System.IO; 3 namespace TEST /* 事件申明、订阅和处理。 */ 4 { 5 public class entsp /* 申明事件类 */ 6 { 7 public delegate void printEventHandler(object sender,printEventArgs e); 8 public event printEventHandler ent; /* 申明事件 */ 9 public void onev() /* 触发事件方法 */ 10 { 11 if ( ent != null ) 12 { 13 ent( this, new printEventArgs("MSG") ); /* 触发事件 */ 14 } 15 } 16 17 18 public static void Main() /* 入口 */ 19 { 20 entsp ee = new entsp(); /* 实例化事件类 */ 21 print pp = new print( ee ); /* 实例化print类 */ 22 ee.onev(); /* 调用触发事件 */ 23 } 24 } 25 public class print /* 申明打印机 */ 26 { 27 public print( entsp sp ) /* 构造函数 */ 28 { 29 sp.ent += p; /* 订阅事件1 */ 30 sp.ent += s; /* 订阅事件2 */ 31 } 32 33 34 public void p( object sender, printEventArgs e ) /* 事件1处理 */ 35 { 36 Console.WriteLine( "pppppp..." ); 37 Console.WriteLine( e.printState ); 38 } 39 40 41 public void s( object sender, EventArgs e ) /* 事件2处理 */ 42 { 43 Console.WriteLine( "sssssss..." ); 44 } 45 } 46 47 public class printEventArgs:EventArgs 48 { 49 public string[] printState {get;set;} 50 51 public printEventArgs(string[] state) 52 { 53 printState = state; 54 } 55 } 56 }
最近正在看事件,上面这货研究了半天,还是没整明白。先存个档,等空了继续。。

浙公网安备 33010602011771号