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

📄 ex-12-02

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
// Example 12-02: Using an array of delegates

namespace Programming_CSharp
{
   using System;

   // the image which we'll manipulate
   public class Image
   {
      public Image()
      {
         Console.WriteLine("An image created");
      }
   }

   public class ImageProcessor
   {
      // declare the delegate
      public delegate void DoEffect();

      // create various static delegates tied to member methods
      public DoEffect BlurEffect = 
         new DoEffect(Blur);
      public DoEffect SharpenEffect = 
         new DoEffect(Sharpen);
      public DoEffect FilterEffect = 
         new DoEffect(Filter);
      public DoEffect RotateEffect = 
         new DoEffect(Rotate);

      // the constructor initializes the image and the array
      public ImageProcessor(Image image)
      {
         this.image = image;
         arrayOfEffects = new DoEffect[10];
      }

      // in a production environment we'd use a more
      // flexible collection.
      public void AddToEffects(DoEffect theEffect)
      {
         if (numEffectsRegistered >= 10)
         {
            throw new Exception(
               "Too many members in array");
         }
         arrayOfEffects[numEffectsRegistered++] 
            = theEffect;
            
      }

      // the image processing methods...
      public static void Blur()
      {
         Console.WriteLine("Blurring image");
      }

      public static void Filter()
      {
         Console.WriteLine("Filtering image");
      }

      public static void Sharpen()
      {
         Console.WriteLine("Sharpening image");
      }

      public static void Rotate()
      {
         Console.WriteLine("Rotating image");
      }



      public void ProcessImages()
      {
         for (int i = 0;i < numEffectsRegistered;i++)
         {
            arrayOfEffects[i]();
         }
      }

      // private member variables...
      private DoEffect[] arrayOfEffects;
      private Image image;
      private int numEffectsRegistered = 0;

   }

   // test driver
   public class Test
   {
      public static void Main()
      {
         Image theImage = new Image();

         // no ui to keep things simple, just pick the
         // methods to invoke, add them in the required
         // order, and then call on the image processor to 
         // run them in the order added.
         ImageProcessor theProc = 
            new ImageProcessor(theImage);
         theProc.AddToEffects(theProc.BlurEffect);
         theProc.AddToEffects(theProc.FilterEffect);
         theProc.AddToEffects(theProc.RotateEffect);
         theProc.AddToEffects(theProc.SharpenEffect);
         theProc.ProcessImages();
      }
   }
}

⌨️ 快捷键说明

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