//1 加参数判断---执行分支逻辑 //2 加方法满足不同的场景 如果方法特别复杂,就推荐这个 /// <summary> /// 既增加公共逻辑方便,又逻辑分离维护简单 鱼肉熊掌怎么兼得? /// 自上往下---逻辑解耦,方便维护升级 /// /// 自下往上---代码重用,去掉重复代码 /// </summary> public void SayHiPerfact(string name, SayHiDelegate method) { //Action<string,int> //Func<string> Console.WriteLine("prepare SayHi.."); method.Invoke(name); }
public void SayHiShow(string name, SayHiDelegate method) { Console.WriteLine("需要先举手"); method.Invoke(name); }
/// <summary> /// 方法三、使用委托 /// </summary> public void SayHiSenior(string name, SayHiDelegate sayHi) { Console.WriteLine("start loging"); sayHi.Invoke(name); Console.WriteLine("end loging"); }
// public void SayHi() // { // SayHiPerfact(" 王大师", this.China); // } public void SayHiPerfact(string name, SayHiDelegate method) { Console.WriteLine("我是通用方法"); method.Invoke(name); }