C# 2.0学习之--代理1

翻译 2006年06月23日 18:37:00

//Copyright (C) Microsoft Corporation.  All rights reserved.

// bookstore.cs
using System;

// A set of classes for handling a bookstore:
namespace Bookstore
{
   using System.Collections;

   // Describes a book in the book list:
   public struct Book
   {
      public string Title;        // Title of the book.
      public string Author;       // Author of the book.
      public decimal Price;       // Price of the book.
      public bool Paperback;      // Is it paperback?

      public Book(string title, string author, decimal price, bool paperBack)
      {
         Title = title;
         Author = author;
         Price = price;
         Paperback = paperBack;
      }
   }

   // Declare a delegate type for processing a book:
   public delegate void ProcessBookDelegate(Book book);

   // Maintains a book database.
   public class BookDB
   {
      // List of all books in the database:
      ArrayList list = new ArrayList();  

      // Add a book to the database:
      public void AddBook(string title, string author, decimal price, bool paperBack)
      {
         list.Add(new Book(title, author, price, paperBack));
      }

      // Call a passed-in delegate on each paperback book to process it:
      public void ProcessPaperbackBooks(ProcessBookDelegate processBook)
      {
         foreach (Book b in list)
         {
            if (b.Paperback)
            // Calling the delegate:
               processBook(b);
         }
      }
   }
}

// Using the Bookstore classes:
namespace BookTestClient
{
   using Bookstore;

   // Class to total and average prices of books:
   class PriceTotaller
   {
      int countBooks = 0;
      decimal priceBooks = 0.0m;

      internal void AddBookToTotal(Book book)
      {
         countBooks += 1;
         priceBooks += book.Price;
      }

      internal decimal AveragePrice()
      {
         return priceBooks / countBooks;
      }
   }

   // Class to test the book database:
   class Test
   {
      // Print the title of the book.
      static void PrintTitle(Book b)
      {
         Console.WriteLine("   {0}", b.Title);
      }

      // Execution starts here.
      static void Main()
      {
         BookDB bookDB = new BookDB();

         // Initialize the database with some books:
         AddBooks(bookDB);     

         // Print all the titles of paperbacks:
         Console.WriteLine("Paperback Book Titles:");
         // Create a new delegate object associated with the static
         // method Test.PrintTitle:
         bookDB.ProcessPaperbackBooks(new ProcessBookDelegate(PrintTitle));

         // Get the average price of a paperback by using
         // a PriceTotaller object:
         PriceTotaller totaller = new PriceTotaller();
         // Create a new delegate object associated with the nonstatic
         // method AddBookToTotal on the object totaller:
         bookDB.ProcessPaperbackBooks(new ProcessBookDelegate(totaller.AddBookToTotal));
         Console.WriteLine("Average Paperback Book Price: ${0:#.##}",
            totaller.AveragePrice());
      }

      // Initialize the book database with some test books:
      static void AddBooks(BookDB bookDB)
      {
         bookDB.AddBook("The C Programming Language",
            "Brian W. Kernighan and Dennis M. Ritchie", 19.95m, true);
         bookDB.AddBook("The Unicode Standard 2.0",
            "The Unicode Consortium", 39.95m, true);
         bookDB.AddBook("The MS-DOS Encyclopedia",
            "Ray Duncan", 129.95m, false);
         bookDB.AddBook("Dogbert's Clues for the Clueless",
            "Scott Adams", 12.00m, true);
      }
   }
}

 


 

csc bookstore.cs
bookstore

相关文章推荐

C# Kinect 2.0 SDK 学习笔记(1)——彩色影像数据流

彩色影像数据流是Kinect摄像头最基本的数据,图像相当于一般的摄像头(废话)。 本节主要是获取Kinect摄像头的彩色影像数据,在Image控件上将其展示出来。 新建WPF项目KinectC...

游戏人生Silverlight(1) - 七彩俄罗斯方块[Silverlight 2.0(c#)]

[源码下载] 游戏人生Silverlight(1) - 七彩俄罗斯方块[Silverlight 2.0(c#)] 作者:webabcd 介绍 使用 Silverligh...

黑马程序员11——C#的委托以及在.NET1.x .NET2.0及.NET3.5写法的演变(未完待续)

---------------------- Windows Phone 7手机开发、.Net培训、 期待与您交流!---------------------- 什么是委托 定义 ...

【转载】C#中的泛型 (From dotNet SDK 2.0 Beta1)

泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性。泛型为.NET框架引入了类型参数(type parameters)的概念。类型参数使得设计类和方法时,不必确定一个或多个具...

C#学习资料之代理与事件

  • 2011-03-11 13:13
  • 215KB
  • 下载

unity3d学习笔记(十八)--利用C#的代理和事件结合NGUI的按钮实现施放技能

本系列文章由Aimar_Johnny编写,欢迎转载,转载请标明出处,谢谢。 http://blog.csdn.net/lzhq1982/article/details/12999381 ...

C#2.0 锐利体验系列课程1

  • 2007-08-23 23:26
  • 7.48MB
  • 下载

基于Android的OpenGL ES 2.0学习笔记(1)

刚开始学习OpenGL ES 2.0,打算先做一个测试apk 在屏幕上点击一个位置后在这个位置画一个点
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)