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

📄 ch5_2.cs

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

public delegate void PrintAnObjectDelegate( object o );

class A
{
   public A()
   {
   }
   public void Print()
   {
      Console.WriteLine("I'm an A!");
   }
}

class B
{
   public B()
   {
   }
   public void Print()
   {
      Console.WriteLine("I'm a B!");
   }
}

class C
{
   public C()
   {
   }
   public void Print()
   {
      Console.WriteLine("I'm a C!");
   }
}


class Printer
{
   
   public PrintAnObjectDelegate dele;
   
   public Printer()
   {
      dele = null;
   }
   public void DoPrint( object o )
   {
      if ( dele != null )
         dele( o );
      
   }
}

class CH5_2
{
   public static void ObjPrint( object o )
   {
      if ( o is A )
      {
         (o as A).Print();
      }
   }
   public static void Main()
   {
      Printer p = new Printer();
      PrintAnObjectDelegate pao = new
         PrintAnObjectDelegate(ObjPrint);
	 
      p.dele = pao;
      A anA = new A();
      B aB = new B();
      C aC = new C();
      
      p.DoPrint( anA );
      p.DoPrint( aB );
      p.DoPrint( aC );
   }
}


⌨️ 快捷键说明

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