C#中委托用法
程序员文章站
2022-06-03 19:08:03
本文实例讲述了c#中委托用法。分享给大家供大家参考。具体分析如下:
对于用户要查找的条件的千变万化,我们在写条件去查找时,是不可能一下写死的,那样,如果你写好了一个类让别...
本文实例讲述了c#中委托用法。分享给大家供大家参考。具体分析如下:
对于用户要查找的条件的千变万化,我们在写条件去查找时,是不可能一下写死的,那样,如果你写好了一个类让别人用,别人需要的不是那种查询,得去找你改条件.
那么我们能否让使用这个类的人自己定义一个规则(条件),直接传条件给你,你帮我查询出结果来,c#就可以用委托来解决,相应的java可以用接口来实现
using system; using system.collections.generic; using system.text; using system.collections; namespace findertest { //性别枚举 public enum genders { male=1,female=2 } //学生类 public class student { public student() { } public student(int _id, string _name, genders _gender, datetime _birthday, string _telephone) { this._id = _id;//学生id this._name = _name;//学生姓名 this._gender = _gender;//学生性别 this._birthday = _birthday;//学生生日 this._telephone = _telephone;//学生电话 } int _id; public int id { get { return _id; } set { _id = value; } } string _name; public string name { get { return _name; } set { _name = value; } } genders _gender; public genders gender { get { return _gender; } set { _gender = value; } } datetime _birthday; public datetime birthday { get { return _birthday; } set { _birthday = value; } } private string _telephone; public string telephone { get { return _telephone; } set { _telephone = value; } } public void show() { console.writeline(string.format("我的姓名:{0}/t学号:{1}/t性别:{2}",_name,_id,_gender)); } } }
using system; using system.collections.generic; using system.text; using system.collections; namespace findertest { //学期枚举 public enum semesters { x1 = 1, x2 = 2, x3 = 3 } public delegate bool predicate(student s);//定义一个委托 //班级类 public class class : arraylist { public class() { } public class(string _name, string _master, semesters _semester) { this._name = _name; this._master = _master; this._semester = _semester; _allstudents = new arraylist(); } private string _name;//班级名字 public string name { get { return _name; } set { _name = value; } } private string _master;//班长 public string master { get { return _master; } set { _master = value; } } private semesters _semester;//学期 public semesters semester { get { return _semester; } set { _semester = value; } } //班级里的学生集合 arraylist _allstudents; public arraylist allstudents { get { return _allstudents; } } public arraylist findall(predicate match) { if (match == null) { return this._allstudents; } arraylist result = new arraylist(); for (int i = 0; i < this._allstudents.count; i++) { student one = (student)this._allstudents[i]; if (match(one)) { result.add(one); } } return result; } } }
using system; using system.collections.generic; using system.text; using system.collections; namespace findertest { class program { static void main(string[] args) { class c1 = new class("0603", "jsp", semesters.x1); student s1 = new student(1, "zs", genders.male, datetime.parse("1988-02-23"), "13088522635"); student s2 = new student(2, "ls", genders.female, datetime.parse("1986-12-03"), "13188522888"); student s3 = new student(3, "ww", genders.female, datetime.parse("1988-11-15"), "13288576885"); student s4 = new student(4, "zl", genders.male, datetime.parse("1984-02-21"), "13388534635"); student s5 = new student(5, "qq", genders.female, datetime.parse("1988-02-23"), "13488524335"); student s6 = new student(6, "cb", genders.male, datetime.parse("1989-02-23"), "13588527636"); c1.allstudents.add(s1); c1.allstudents.add(s2); c1.allstudents.add(s3); c1.allstudents.add(s4); c1.allstudents.add(s5); c1.allstudents.add(s6); arraylist list= c1.findall(match); //查找班级女生的资料 // arraylist list = c1.findall(match1); //查找学号从1到5的学生 foreach (student s in list) { s.show(); } } //条件为女性 public static bool match(student s) { if (s.gender.equals(genders.female)) { return true; } return false; } //条件为学号从1到5 public static bool match1(student s) { if (s.id.compareto(1) >= 0 && s.id.compareto(5) <= 0) { return true; } return false; } } }
希望本文所述对大家的c#程序设计有所帮助。
上一篇: 鳕鱼怎么做,这样做最好吃
下一篇: C#创建IIS虚拟目录的方法