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

手把手教你实现嵌入式SNMP代理第1部分

-
  • 1970年01月01日 08:00

搭建nghttp2.0作为apns服务的代理的相关问题总结

apple新一代apns推送采用http2.0,升级公司apns服务,决定采用nghttpx作为apns的代理。在搭建这个服务的过程中遇到了两个坑,分享一下我的经验。...
  • KeNanXiuJi
  • KeNanXiuJi
  • 2016-09-19 19:41:27
  • 1919

【第一章】C#的进化史——C#1.0,2.0,3.0区别比较(一)

一个产品(Product)类,通过该类的声明来观察C#的进化 C# 1.0版 public class Product { string name; pu...
  • qjzh
  • qjzh
  • 2012-02-11 19:25:01
  • 1912

C# 2.0学习之--代理2

//Copyright (C) Microsoft Corporation.  All rights reserved.// compose.csusing System;delegate void ...
  • iiboy
  • iiboy
  • 2006-06-23 18:38:00
  • 854

C# 2.0学习之--匿名代理

  编译与执行: csc AnonymousDelegates.csAnonymousDelegates ...
  • iiboy
  • iiboy
  • 2006-06-23 18:09:00
  • 1356

C# Kinect 2.0 SDK 学习笔记(0)——基本情况介绍

去年十一的时候凑热闹买了个国行版的Xbox One,玩了一阵子之后开始对摄像头感兴趣,咬咬牙又买了个Adapter,把Kinect二代摄像头接到了电脑上,上网找了很多资料,官方2.0的SDK已经放出,...
  • ivanyoung66
  • ivanyoung66
  • 2015-07-05 21:39:55
  • 2385

Qt 学习之路 2(86):QML视图代理

与 Qt model/view 架构类似,在自定义用户界面中,代理扮演着重要的角色。模型中的每一个数据项都要通过一个代理向用户展示,事实上,用户看到的可视部分就是代理。 每一个代理都...
  • lmhuanying1012
  • lmhuanying1012
  • 2017-10-09 00:07:45
  • 180

asp.net2.0学习历程 菜鸟到中级程序员的飞跃【月儿原创】

asp.net2.0学习历程 菜鸟到中级程序员的飞跃--30本好书点评作者:清清月儿主页:http://blog.csdn.net/21aspnet/          时间:2007.5.16学历历...
  • 21aspnet
  • 21aspnet
  • 2007-05-16 23:34:00
  • 57254

认识django2.0读书笔记(1)---第一章 介绍

文档下载地址:Django_2.0_中文教程  http://download.csdn.net/detail/julius_lee/6620099 在线地址:http://djangobook.p...
  • julius_lee
  • julius_lee
  • 2013-11-27 16:30:27
  • 1472

Spring Boot 2.0.0.M7 调用REST服务-如何使用代理

更多干货spring-boot系列一 之restfull api与多环境配置springboot系列二之 日志SpringBoot系列三之 MVC 模版引擎SpringBoot 2.0.0.M7 系列...
  • qq_27384769
  • qq_27384769
  • 2018-01-12 07:44:01
  • 480
收藏助手
不良信息举报
您举报文章:C# 2.0学习之--代理1
举报原因:
原因补充:

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