C#中委托用法
本文實例講述了C#中委托用法。分享給大家供大家參考。具體分析如下:
對于用戶要查找的條件的千變?nèi)f化,我們在寫條件去查找時,是不可能一下寫死的,那樣,如果你寫好了一個類讓別人用,別人需要的不是那種查詢,得去找你改條件.
那么我們能否讓使用這個類的人自己定義一個規(guī)則(條件),直接傳條件給你,你幫我查詢出結(jié)果來,C#就可以用委托來解決,相應的java可以用接口來實現(xiàn)
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#程序設(shè)計有所幫助。
相關(guān)文章
C#使用NAudio實現(xiàn)監(jiān)聽系統(tǒng)聲音
這篇文章主要為大家詳細介紹了C#如何使用NAudio實現(xiàn)監(jiān)聽系統(tǒng)聲音并屏蔽麥克風其他聲音,文中的示例代碼講解詳細,有需要的小伙伴可以參考下2024-02-02C#中Foreach循環(huán)遍歷的本質(zhì)與枚舉器詳解
這篇文章主要給大家介紹了關(guān)于C#中Foreach循環(huán)遍歷本質(zhì)與枚舉器的相關(guān)資料,foreach循環(huán)用于列舉出集合中所有的元素,foreach語句中的表達式由關(guān)鍵字in隔開的兩個項組成,本文通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-08-08- RSA解決了對稱加密的一個不足,比如AES算法加密和解密時使用的是同一個秘鑰,因此這個秘鑰不能公開,因此對于需要公開秘鑰的場合,我們需要在加密和解密過程中使用不同的秘鑰,加密使用的公鑰可以公開,解密使用的私鑰要保密,這就是非對稱加密的好處?!?/div> 2021-06-06
unity 實現(xiàn)攝像機繞某點旋轉(zhuǎn)一周
這篇文章主要介紹了unity 實現(xiàn)攝像機繞某點旋轉(zhuǎn)一周,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04協(xié)定需要會話,但是綁定“BasicHttpBinding”不支持它或者因配置不正確而無法支持它
在IIS7及以上版本服務器中提供了基于WAS的無.SVC文件的WCF服務激活功能,能夠提供基于HTTP和非HTTP協(xié)議的訪問,通過添加Windows Server AppFabric可以更方便的管理WCF服務2012-12-12最新評論