⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch5_6.cs

📁 《c#技术内幕代码》
💻 CS
字号:
using System;
using System.Collections;

public delegate bool HandleKeyword( string key );

public class Parser
{
   Hashtable parseTable;
   
   public Parser()
   {
      parseTable = new Hashtable();
   }
   
   public void AddKeywordHandler( string key, HandleKeyword handler )
   {
      parseTable.Add( key, handler );
   }
   
   public bool ParseKeyword( string key )
   {
      HandleKeyword func = (HandleKeyword)parseTable[ key ];
      if ( func == null )
         return false;
	 
      return func( key );
   }
}

public class HandleHello
{
   public static bool HandleIt( string s )
   {
      Console.WriteLine( "HandleHello::HandleIt {0}", s );
      return true;
   }
}

public class HandleGoodbye
{
   public static bool HandleIt( string s )
   {
      Console.WriteLine( "HandleGooebye::HandleIt {0}", s );
      return true;
   }
}

public class HandleWhy
{
   public static bool HandleIt( string s )
   {
      Console.WriteLine( "HandleWhy::HandleIt {0}", s );
      return true;
   }
}

class CH5_6
{
   public static void Main(string[] args)
   {
      // Create the objects
      Parser p = new Parser();
      
      // Add the handlers
      p.AddKeywordHandler( "hello", new HandleKeyword(HandleHello.HandleIt) );
      p.AddKeywordHandler( "goodbye", new HandleKeyword(HandleGoodbye.HandleIt) );
      p.AddKeywordHandler( "why", new HandleKeyword(HandleWhy.HandleIt) );
      
      // Do the parsing
      for ( int i=0; i<args.Length; ++i )
         if ( p.ParseKeyword( args[i] ) == false )
	    Console.WriteLine("Unknown keyword {0}", args[i] );
   }
}



      
      

   
   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -