欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中委托用法

 更新時間:2015年05月28日 17:55:10   作者:jayqean  
這篇文章主要介紹了C#中委托用法,實例分析了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#實現(xiàn)在屏幕上畫圖效果的代碼實例

    使用C#實現(xiàn)在屏幕上畫圖效果的代碼實例

    本篇文章是對使用C#在屏幕上畫圖效果的實現(xiàn)方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C#使用NAudio實現(xiàn)監(jiān)聽系統(tǒng)聲音

    C#使用NAudio實現(xiàn)監(jiān)聽系統(tǒng)聲音

    這篇文章主要為大家詳細介紹了C#如何使用NAudio實現(xiàn)監(jiān)聽系統(tǒng)聲音并屏蔽麥克風其他聲音,文中的示例代碼講解詳細,有需要的小伙伴可以參考下
    2024-02-02
  • C#中Foreach循環(huán)遍歷的本質(zhì)與枚舉器詳解

    C#中Foreach循環(huán)遍歷的本質(zhì)與枚舉器詳解

    這篇文章主要給大家介紹了關(guān)于C#中Foreach循環(huán)遍歷本質(zhì)與枚舉器的相關(guān)資料,foreach循環(huán)用于列舉出集合中所有的元素,foreach語句中的表達式由關(guān)鍵字in隔開的兩個項組成,本文通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-08-08
  • C#設(shè)計模式之裝飾器模式實例詳解

    C#設(shè)計模式之裝飾器模式實例詳解

    本文詳細講解了C#設(shè)計模式之裝飾器模式,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • c# 實現(xiàn)RSA非對稱加密算法

    c# 實現(xiàn)RSA非對稱加密算法

    RSA解決了對稱加密的一個不足,比如AES算法加密和解密時使用的是同一個秘鑰,因此這個秘鑰不能公開,因此對于需要公開秘鑰的場合,我們需要在加密和解密過程中使用不同的秘鑰,加密使用的公鑰可以公開,解密使用的私鑰要保密,這就是非對稱加密的好處?!?/div> 2021-06-06
  • C#如何實現(xiàn)圖片的剪裁并保存

    C#如何實現(xiàn)圖片的剪裁并保存

    基于c#實現(xiàn)圖片的裁剪并保存功能,實現(xiàn)方法非常簡單的,前端采用的cropper插件,但是在本文中沒有給大家多介紹,需要的朋友可以到腳本之家去查找這個插件。好了,如果大家對c#實現(xiàn)圖片裁剪并保存功能感興趣的朋友一起看看吧
    2016-11-11
  • 關(guān)于C#中ajax跨域訪問問題

    關(guān)于C#中ajax跨域訪問問題

    最近做項目,需要跨域請求訪問數(shù)據(jù)問題。下面通過本文給大家分享C#中ajax跨域訪問代碼詳解,需要的朋友可以參考下
    2017-05-05
  • C#實現(xiàn)啟動項管理的示例代碼

    C#實現(xiàn)啟動項管理的示例代碼

    這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)啟動項管理,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以了解一下
    2022-12-12
  • unity 實現(xiàn)攝像機繞某點旋轉(zhuǎn)一周

    unity 實現(xiàn)攝像機繞某點旋轉(zhuǎn)一周

    這篇文章主要介紹了unity 實現(xiàn)攝像機繞某點旋轉(zhuǎn)一周,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 協(xié)定需要會話,但是綁定“BasicHttpBinding”不支持它或者因配置不正確而無法支持它

    協(xié)定需要會話,但是綁定“BasicHttpBinding”不支持它或者因配置不正確而無法支持它

    在IIS7及以上版本服務器中提供了基于WAS的無.SVC文件的WCF服務激活功能,能夠提供基于HTTP和非HTTP協(xié)議的訪問,通過添加Windows Server AppFabric可以更方便的管理WCF服務
    2012-12-12

最新評論