C#中委托用法
本文实例讲述了C#中委托用法。分享给大家供大家参考。具体分析如下:
对于用户要查找的条件的千变万化,我们在写条件去查找时,是不可能一下写死的,那样,如果你写好了一个类让别人用,别人需要的不是那种查询,得去找你改条件.
那么我们能否让使用这个类的人自己定义一个规则(条件),直接传条件给你,你帮我查询出结果来,C#就可以用委托来解决,相应的java可以用接口来实现
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Collections;
namespaceFinderTest
{
//性别枚举
publicenumGenders
{
male=1,female=2
}
//学生类
publicclassStudent
{
publicStudent()
{}
publicStudent(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;
publicintId
{
get{return_id;}
set{_id=value;}
}
string_name;
publicstringName
{
get{return_name;}
set{_name=value;}
}
Genders_gender;
publicGendersGender
{
get{return_gender;}
set{_gender=value;}
}
DateTime_birthday;
publicDateTimeBirthday
{
get{return_birthday;}
set{_birthday=value;}
}
privatestring_telephone;
publicstringTelephone
{
get{return_telephone;}
set{_telephone=value;}
}
publicvoidshow()
{
Console.WriteLine(string.Format("我的姓名:{0}/t学号:{1}/t性别:{2}",_name,_id,_gender));
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Collections;
namespaceFinderTest
{
//学期枚举
publicenumSemesters
{
x1=1,x2=2,x3=3
}
publicdelegateboolPredicate(Students);//定义一个委托
//班级类
publicclassClass:ArrayList
{
publicClass()
{}
publicClass(string_name,string_master,Semesters_semester)
{
this._name=_name;
this._master=_master;
this._semester=_semester;
_allStudents=newArrayList();
}
privatestring_name;//班级名字
publicstringName
{
get{return_name;}
set{_name=value;}
}
privatestring_master;//班长
publicstringMaster
{
get{return_master;}
set{_master=value;}
}
privateSemesters_semester;//学期
publicSemestersSemester
{
get{return_semester;}
set{_semester=value;}
}
//班级里的学生集合
ArrayList_allStudents;
publicArrayListAllStudents
{
get{return_allStudents;}
}
publicArrayListFindAll(Predicatematch)
{
if(match==null)
{
returnthis._allStudents;
}
ArrayListresult=newArrayList();
for(inti=0;i<this._allStudents.Count;i++)
{
Studentone=(Student)this._allStudents[i];
if(match(one))
{
result.Add(one);
}
}
returnresult;
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Collections;
namespaceFinderTest
{
classProgram
{
staticvoidMain(string[]args)
{
Classc1=newClass("0603","jsp",Semesters.x1);
Students1=newStudent(1,"zs",Genders.male,DateTime.Parse("1988-02-23"),"13088522635");
Students2=newStudent(2,"ls",Genders.female,DateTime.Parse("1986-12-03"),"13188522888");
Students3=newStudent(3,"ww",Genders.female,DateTime.Parse("1988-11-15"),"13288576885");
Students4=newStudent(4,"zl",Genders.male,DateTime.Parse("1984-02-21"),"13388534635");
Students5=newStudent(5,"qq",Genders.female,DateTime.Parse("1988-02-23"),"13488524335");
Students6=newStudent(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);
ArrayListlist=c1.FindAll(match);
//查找班级女生的资料
//ArrayListlist=c1.FindAll(match1);
//查找学号从1到5的学生
foreach(Studentsinlist)
{
s.show();
}
}
//条件为女性
publicstaticboolmatch(Students)
{
if(s.Gender.Equals(Genders.female))
{
returntrue;
}
returnfalse;
}
//条件为学号从1到5
publicstaticboolmatch1(Students)
{
if(s.Id.CompareTo(1)>=0&&s.Id.CompareTo(5)<=0)
{
returntrue;
}
returnfalse;
}
}
}
希望本文所述对大家的C#程序设计有所帮助。