为了容易理解,方法名使用了中文名称, 这是一个用2005开发的DEMO,与2003一样,只要你将public partial class改成public class就可以了。 这个例子主要展现了Delegate的用法,超过60分的,将给与及格的评价,低于60分的,给于尚需努力的评价。 ///-------------------------form1.cs-------------------------/// using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace HelloWorldCS { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Student s = new Student(); s.adviseDelegate = new Student.AdviseDelegate(Teacher.评分); label1.Text = s.给分获得评价(50); label2.Text = s.给分获得评价(70); } } }///-------------------------student.cs-------------------------///using System; using System.Collections.Generic; using System.Text;namespace HelloWorldCS { class Student { public delegate string AdviseDelegate(int i); public AdviseDelegate adviseDelegate; public string 给分获得评价(int score) { if (adviseDelegate != null) { return adviseDelegate(score); } else { return "AdviseDelegate is null!";
}
} } } ///-------------------------teacher.cs-------------------------/// using System; using System.Collections.Generic; using System.Text;namespace HelloWorldCS { static class Teacher { public static string 评分(int score) { if (score > 60) return "你合格了!"; else return "还须努力!";
这是一个用2005开发的DEMO,与2003一样,只要你将public partial class改成public class就可以了。
这个例子主要展现了Delegate的用法,超过60分的,将给与及格的评价,低于60分的,给于尚需努力的评价。
///-------------------------form1.cs-------------------------///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace HelloWorldCS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ Student s = new Student(); s.adviseDelegate = new Student.AdviseDelegate(Teacher.评分); label1.Text = s.给分获得评价(50); label2.Text = s.给分获得评价(70); }
}
}///-------------------------student.cs-------------------------///using System;
using System.Collections.Generic;
using System.Text;namespace HelloWorldCS
{
class Student
{
public delegate string AdviseDelegate(int i); public AdviseDelegate adviseDelegate; public string 给分获得评价(int score)
{ if (adviseDelegate != null)
{
return adviseDelegate(score); }
else { return "AdviseDelegate is null!";
}
}
}
}
///-------------------------teacher.cs-------------------------///
using System;
using System.Collections.Generic;
using System.Text;namespace HelloWorldCS
{
static class Teacher
{
public static string 评分(int score)
{ if (score > 60) return "你合格了!"; else return "还须努力!";
} }
}