.net事件模型

View Code
   
     
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main( string [] args)
{
BookStore bs
= new BookStore();
bs.AfterAddedBook
+= (Object b) => { Book bb = (Book)b; Console.WriteLine(bb.name + " @@ " + bb.prise); };
bs.AfterAddedBook
+= new BookStore.BookHandle( new Program().tip);
Book book
= new Book();
book.name
= " C#编程设计 " ;
book.prise
= 100 ;
bs.AddInBook(book);
}
void tip(Object b)
{
Book bb
= (Book)b;
Console.WriteLine(bb.name
+ " @ " + bb.prise + " 新书 入库 " );
}
}


public class BookStore
{
public List < Book > books { get ; set ; }
public delegate void BookHandle(Object obj);
public event BookHandle AfterAddedBook;
public BookStore()
{
this .books = new List < Book > ();
}
public void AddInBook(Book b)
{
books.Add(b);
if (AfterAddedBook != null )
{
AfterAddedBook(b);
}
}

}
public class Book
{
public string name { get ; set ; }
public int prise { get ; set ; }
}
}

你可能感兴趣的:(.net)