using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace detegateClass2
{
class StudentDelegate
{
private int score;
public delegate string DelegateResponse(int score);
public DelegateResponse delegateInstance;
public void setScore(int score)
{
this.score = score;
if (score > 100 || score < 0)
{
Console.WriteLine("分数不正确");
}
else
{
string result;
result = delegateInstance(score);//这里采用了委托的方法取代上个版本的接口的方法...
Console.WriteLine("学生受到的回复数 : "+ result);
}
}
}
class TeacherDelegate
{
public string response(int score)
{
if (score > 60)
{
return "不错!! " + score + "分,是个好分数";
}
else
{
return "加油!! " + score + "分,证明你还有很大的进步空间!";
}
}
}
/// <summary>
/// 这里采用委托的方法来模拟学生在考试后,收到来自家长.老师.同学等的回复的事情
/// </summary>
class DelegateClass
{
/// <summary>
/// 委托代理的好处是现在做出动作的对象不用再继承接口了...更加的面向对象
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
StudentDelegate s = new StudentDelegate();
TeacherDelegate t = new TeacherDelegate();
//委托不同接口的体现在:委托包装的方法,,任何动作的接收方例如Student,不用再定义setX的方法了
s.delegateInstance = new StudentDelegate.DelegateResponse(t.response);
s.setScore(20);
}
}
}