C#中委托用法

所属分类: 软件编程 / C#教程 阅读数: 65
收藏 0 赞 0 分享

本文实例讲述了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#抽象类与抽象方法详解

这篇文章主要为大家详细介绍了C#抽象类与抽象方法的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C#代码实现扑克牌排序的几种方式

今天小编就为大家分享一篇关于C#代码实现扑克牌排序,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#泛型概念的简介与泛型的使用

今天小编就为大家分享一篇关于C#泛型概念的简介与泛型的使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C# 7.0 使用下划线忽略使用的变量的原因分析

这篇文章主要介绍了C# 7.0 使用下划线忽略使用的变量的原因浅析,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C# 中使用正则表达式匹配字符的含义

正则表达式的作用用来描述字符串的特征。本文重点给大家介绍C# 中使用正则表达式匹配字符的含义,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

C# Dictionary和SortedDictionary的简介

今天小编就为大家分享一篇关于C# Dictionary和SortedDictionary的简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#中SQL Command的基本用法

今天小编就为大家分享一篇关于C#中SQL Command的基本用法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL DataReader访问数据的优点和实例

今天小编就为大家分享一篇关于C#使用SQL DataReader访问数据的优点和实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL Dataset数据集代码实例

今天小编就为大家分享一篇关于的文章,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL DataAdapter数据适配代码实例

今天小编就为大家分享一篇关于C#使用SQL DataAdapter数据适配代码实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享
查看更多