using System; using System.Collections; using System.Collections.Generic; using System.IO; namespace codeTest { delegate void NumDelegate(int num); class Program { static void Main(string[] args) { AClass _a = new AClass(); _a.cwdelegate = _a.AddNum; _a.cwdelegate(20); NumDelegate num20delegate = new NumDelegate(Add20); Console.WriteLine("-------------------------------------------"); NumDelegate adddelegate =new NumDelegate(_a.AddNum); NumDelegate subdelegate = new NumDelegate(_a.SubNum); NumDelegate num30delegate = adddelegate + subdelegate; num30delegate(30); Console.WriteLine("-------------------------------------------"); num30delegate += num20delegate; num30delegate(30); Console.WriteLine("-------------------------------------------"); num30delegate += adddelegate; //去掉最后一个adddelegate num30delegate -= adddelegate; num30delegate(30); Console.ReadLine(); } static void Add20(int num) { Console.WriteLine(string.Format("Add20: {0}", num)); } } class AClass { public int result = 100; public delegate void CWDelegate(int num); public CWDelegate cwdelegate; public void AddNum(int num) { Console.WriteLine(string.Format("AddNum: {0}",num)); } public void SubNum(int num) { Console.WriteLine(string.Format("SubNum: {0}", num)); } } }
时间: 2024-10-17 05:31:38