C#中拜托用法。本站提示廣大學習愛好者:(C#中拜托用法)文章只能為提供參考,不一定能成為您想要的結果。以下是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#法式設計有所贊助。