基本类型--委托(一)
15.1初识委托
15.2使用委托来回调静态方法
15.3使用委托回调实例方法
15.4委托揭秘
15.5使用委托回调多个方法(链式)
//声明一个委托,它的实例引用一个方法,该方法接受一个Int32的参数并返回void
internal delegate void Feedback(Int32 value);
public sealed class Program {
public static void Main() {
StaticDelegateDemo();
InstanceDelegateDemo();
ChainDelegateDemo1(new Program());
ChainDelegateDemo2(new Program());
}
//委托调用静态回调方法
private static void StaticDelegateDemo() {
Console.WriteLine("---StaticDelegateDemo---");
Counter(1, 3, null);
Counter(1, 3, new Feedback(FeedBackToConsole));
Counter(1, 3, new Feedback(FeedBackToMsgBox));
Console.ReadLine();
}
//委托调用实例回调方法
private static void InstanceDelegateDemo() {
Console.WriteLine("---InstanceDelegateDemo---");
Program p = new Program();
Counter(1, 3, new Feedback(p.FeedBackToFile));
Console.ReadLine();
}
//顺序调用多个静态、实例方法
private static void ChainDelegateDemo1(Program p) {
Console.WriteLine("---ChainDelegateDemo1---");
Feedback fb1 = new Feedback(FeedBackToConsole);
Feedback fb2 = new Feedback(FeedBackToMsgBox);
Feedback fb3 = new Feedback(p.FeedBackToFile);
Feedback fbChain = null;
fbChain = (Feedback)Delegate.Combine(fbChain, fb1);
fbChain = (Feedback)Delegate.Combine(fbChain, fb2);
fbChain = (Feedback)Delegate.Combine(fbChain, fb3);
Counter(4, 5, fbChain);
Console.WriteLine();
fbChain = (Feedback)Delegate.Remove(fbChain, new Feedback(FeedBackToMsgBox));
Counter(6, 7, fbChain);
Console.ReadLine();
}
//顺序调用多个静态、实例方法
private static void ChainDelegateDemo2(Program p) {
Console.WriteLine("---ChainDelegateDemo2---");
Feedback fb1 = new Feedback(FeedBackToConsole);
Feedback fb2 = new Feedback(FeedBackToMsgBox);
Feedback fb3 = new Feedback(p.FeedBackToFile);
Feedback fbChain = null;
fbChain += fb1;
fbChain += fb2;
fbChain += fb3;
Counter(8, 9, fbChain);
Console.WriteLine();
fbChain -= new Feedback(FeedBackToMsgBox);
Counter(10, 11, fbChain);
Console.ReadLine();
}
//
private static void Counter(Int32 from, Int32 to, Feedback fb) {
for (int i = from; i <= to; i++){
if (fb != null)
fb(i);
}
}
private static void FeedBackToConsole(Int32 value) {
Console.WriteLine("控制台输出:" + value);
}
private static void FeedBackToMsgBox(Int32 value) {
Console.WriteLine("消息框显示:" + value);
}
private void FeedBackToFile(Int32 value) {
StreamWriter sw = new StreamWriter("C:/FeedBackToFile.txt", true);
sw.WriteLine("文件显示:" + value);
sw.Close();
}
}
internal delegate void Feedback(Int32 value);
public sealed class Program {
public static void Main() {
StaticDelegateDemo();
InstanceDelegateDemo();
ChainDelegateDemo1(new Program());
ChainDelegateDemo2(new Program());
}
//委托调用静态回调方法
private static void StaticDelegateDemo() {
Console.WriteLine("---StaticDelegateDemo---");
Counter(1, 3, null);
Counter(1, 3, new Feedback(FeedBackToConsole));
Counter(1, 3, new Feedback(FeedBackToMsgBox));
Console.ReadLine();
}
//委托调用实例回调方法
private static void InstanceDelegateDemo() {
Console.WriteLine("---InstanceDelegateDemo---");
Program p = new Program();
Counter(1, 3, new Feedback(p.FeedBackToFile));
Console.ReadLine();
}
//顺序调用多个静态、实例方法
private static void ChainDelegateDemo1(Program p) {
Console.WriteLine("---ChainDelegateDemo1---");
Feedback fb1 = new Feedback(FeedBackToConsole);
Feedback fb2 = new Feedback(FeedBackToMsgBox);
Feedback fb3 = new Feedback(p.FeedBackToFile);
Feedback fbChain = null;
fbChain = (Feedback)Delegate.Combine(fbChain, fb1);
fbChain = (Feedback)Delegate.Combine(fbChain, fb2);
fbChain = (Feedback)Delegate.Combine(fbChain, fb3);
Counter(4, 5, fbChain);
Console.WriteLine();
fbChain = (Feedback)Delegate.Remove(fbChain, new Feedback(FeedBackToMsgBox));
Counter(6, 7, fbChain);
Console.ReadLine();
}
//顺序调用多个静态、实例方法
private static void ChainDelegateDemo2(Program p) {
Console.WriteLine("---ChainDelegateDemo2---");
Feedback fb1 = new Feedback(FeedBackToConsole);
Feedback fb2 = new Feedback(FeedBackToMsgBox);
Feedback fb3 = new Feedback(p.FeedBackToFile);
Feedback fbChain = null;
fbChain += fb1;
fbChain += fb2;
fbChain += fb3;
Counter(8, 9, fbChain);
Console.WriteLine();
fbChain -= new Feedback(FeedBackToMsgBox);
Counter(10, 11, fbChain);
Console.ReadLine();
}
//
private static void Counter(Int32 from, Int32 to, Feedback fb) {
for (int i = from; i <= to; i++){
if (fb != null)
fb(i);
}
}
private static void FeedBackToConsole(Int32 value) {
Console.WriteLine("控制台输出:" + value);
}
private static void FeedBackToMsgBox(Int32 value) {
Console.WriteLine("消息框显示:" + value);
}
private void FeedBackToFile(Int32 value) {
StreamWriter sw = new StreamWriter("C:/FeedBackToFile.txt", true);
sw.WriteLine("文件显示:" + value);
sw.Close();
}
}
15.2使用委托来回调静态方法
15.3使用委托回调实例方法
15.4委托揭秘
15.5使用委托回调多个方法(链式)
作者:青羽