1// bookstore.cs 2using System; 3// A set of classes for handling a bookstore: 4namespace Bookstore 5{ 6using System.Collections; 7// Describes a book in the book list: 8publicstruct Book 9{ 10publicstring Title; // Title of the book. 11publicstring Author; // Author of the book. 12publicdecimal Price; // Price of the book. 13publicbool Paperback; // Is it paperback? 14public Book(string title, string author, decimal price, bool paperBack) 15{ 16 Title = title; 17 Author = author; 18 Price = price; 19 Paperback = paperBack; 20 } 21 } 22 23// Declare a delegate type for processing a book: 24publicdelegatevoid ProcessBookDelegate(Book book); 25 26// Maintains a book database. 27publicclass BookDB 28{ 29// List of all books in the database: 30 ArrayList list =new ArrayList(); 31 32// Add a book to the database: 33publicvoid AddBook(string title, string author, decimal price, bool paperBack) 34{ 35 list.Add(new Book(title, author, price, paperBack)); 36 } 37 38// Call a passed-in delegate on each paperback book to process it: 39publicvoid ProcessPaperbackBooks(ProcessBookDelegate processBook) 40{ 41foreach (Book b in list) 42{ 43if (b.Paperback) 44 45// Calling the delegate: 46 processBook(b); 47 } 48 } 49 } 50} 51// Using the Bookstore classes: 52namespace BookTestClient 53{ 54using Bookstore; 55 56// Class to total and average prices of books: 57class PriceTotaller 58{ 59int countBooks =0; 60decimal priceBooks =0.0m; 61internalvoid AddBookToTotal(Book book) 62{ 63 countBooks +=1; 64 priceBooks += book.Price; 65 } 66internaldecimal AveragePrice() 67{ 68return priceBooks / countBooks; 69 } 70 } 71// Class to test the book database: 72class Test 73{ 74// Print the title of the book. 75staticvoid PrintTitle(Book b) 76{ 77 Console.WriteLine(" {0}", b.Title); 78 } 79// Execution starts here. 80staticvoid Main() 81{ 82 BookDB bookDB =new BookDB(); 83// Initialize the database with some books: 84 AddBooks(bookDB); 85// Print all the titles of paperbacks: 86 Console.WriteLine("Paperback Book Titles:"); 87// Create a new delegate object associated with the static 88// method Test.PrintTitle: 89 bookDB.ProcessPaperbackBooks(new ProcessBookDelegate(PrintTitle)); 90// Get the average price of a paperback by using 91// a PriceTotaller object: 92 PriceTotaller totaller =new PriceTotaller(); 93// Create a new delegate object associated with the nonstatic 94// method AddBookToTotal on the object totaller: 95 bookDB.ProcessPaperbackBooks(new ProcessBookDelegate(totaller.AddBookToTotal)); 96 Console.WriteLine("Average Paperback Book Price: ${0:#.##}", 97 totaller.AveragePrice()); 98 } 99// Initialize the book database with some test books: 100staticvoid AddBooks(BookDB bookDB) 101{ 102 bookDB.AddBook("The C Programming Language", 103"Brian W. Kernighan and Dennis M. Ritchie", 19.95m, true); 104 bookDB.AddBook("The Unicode Standard 2.0", 105"The Unicode Consortium", 39.95m, true); 106 bookDB.AddBook("The MS-DOS Encyclopedia", 107"Ray Duncan", 129.95m, false); 108 bookDB.AddBook("Dogbert's Clues for the Clueless", 109"Scott Adams", 12.00m, true); 110 } 111 } 112} 113
输出:
平装书的标题:
The C Programming Language The Unicode Standard 2.0
Dogbert's Clues for the Clueless
平均价格: $23.97
讨论:
委托的声明 委托可声明如下: public delegate void ProcessBookDelegate(Book book); 声明一个新的委托类型。每个委托类型可包含委托描述的数量和类型,包含被压缩方法返回值的类型。不管是否需要一组类型描述或返回值类型,必须声明一个新的委托类型。