C#中委託用法

來源:互聯網
上載者:User

   本文執行個體講述了C#中委託用法。分享給大家供大家參考。具體分析如下:

  對於使用者要尋找的條件的千變萬化,我們在寫條件去尋找時,是不可能一下寫死的,那樣,如果你寫好了一個類讓別人用,別人需要的不是那種查詢,得去找你改條件.

  那麼我們能否讓使用這個類的人自己定義一個規則(條件),直接傳條件給你,你幫我查詢出結果來,C#就可以用委託來解決,相應的java可以用介面來實現

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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)); } } }

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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; } } }

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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#程式設計有所協助。

相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。