C # 2.0 Learning - Agent 1

xiaoxiao2021-04-08  412

// Copyright (c) Microsoft Corporation. All Rights Reserved.

// bookstore.csusing 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 Price of Books: Class Pricetary {INT CountBooks = 0; Decimal Pricebooks = 0.0m;

INTERNAL VOID ADDBOOKTOTAl (book book) {CountBooks = 1; Pricebooks = BOOK.PRICE;} INTERNAL DECIMAL AVICEPRICE ()}}

// Class to Test The Book Database: Class Test {// Print The title of the book. Static voidprinttitle (book b) {console.writeline ("{0}", b.title);}

// Execution starts here. Static void main () {bookdb boxdb = 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.Addbooktota); 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

转载请注明原文地址:https://www.9cbs.com/read-132882.html

New Post(0)