• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
冥海
博客园    首页    新随笔    联系   管理    订阅  订阅

我看委托(一)

1.委托
(1).如何声明一个委托?
delegate 返回值类型  委托类型(参数)
delegate void saydelegate(string s);

(2).委托是如何指向方法的?对方法进行调用
★★★委托类型声明的委托对象指向方法
★★★委托对象调用委托的方法(看方法的参数进行调用)

委托要调用方法,肯定要有方法被委托进行调用。
可以有以下两种方法进行调用
①委托类型   委托对象  实例化委托(传参数的名称即方法的名称)
saydelegate saydelegate =new saydelegate(Say); //先要指向方法
//saydelegate("123");                          //然后才能进行调用


②saydelegate saydelegate1 = Say;     //如果委托的对象没有指向方法,没有进行调用,缺少一步都不行。
//saydelegate("123");


(3).在方法里调用委托
①声明一个委托  delegate bool adelegate(int i); 委托的返回值类型,参数类型要和委托调用的方法返回值类型,参数类型一致

②声明一个委托调用的方法
static bool say(int i)
{
  return i > 20;
}

③在方法中进行调用委托
static List<int> list3(List<int> list1, adelegate f)
{
     List<int> list2 = new List<int>();
     foreach (int i in list1)
     {
        if (f(i))
        {
         list2.Add(i);
        }

      }
    
      return list2;
}

④代码中调用调用委托的函数
List<int> newList =list3(list1,say);

(4).简单的在代码中声明委托
①//声明一个委托
public delegate bool adelegate(string s);

②声明委托需要执行的方法
static bool Say(string s)
{
     if (s == string.Empty)
      {
        MessageBox.Show("请输入文本");
        return false;
      }
   
     else
      {
        return true;
      }

}

③在需要执行委托对象的声明
public adelegate adelegate1;

④在需要用委托调用方法的地方进行调用声明
adelegate1 = new adelegate(Say);
if (adelegate1(textBox1.Text.Trim()) == false)
{
  textBox1.Focus();
}

(5).委托的复杂应用
第一步:声明一个委托
public delegate void delegate1(A a);

第二步:在用户控件的事件时,进行调用
private void button1_Click(object sender, EventArgs e)
{
    if (adelegate != null)
   {
       A a = new A();
       a.value = textBox1.Text.Trim();
       a.isvalue = true;
       adelegate(a);
       if (a.isvalue == false)
     {
        MessageBox.Show("数据错误");
     }
  }
}


第三步:在窗体中进行监听,监听调用方法   在别的地方进行监听,如果听到,则把方法传给委托调用的方法。
Clientname.adelegate = Say; //把调用的方法传给委托调用的方法

第四步:声明监听的方法
void Say(A a)
{
    if (a.value.Length <= 2)
    {
      a.isvalue = false;
    }
}


(6).如何引入事件的概念
①那么存不存在清除监听和冒充监听呢?

第一步:声明委托
public delegate void adelegate();

第二步:在需要的地方执行委托
private void button1_Click(object sender, EventArgs e)
{
   if (adele != null)
   {
       adele();
   }
}

第三步:委托调用方法 控件名.委托对象
userName.adele +=new adelegate(Say);
userName.adele += new adelegate(Hellow);


第四步:声明委托调用的方法
void Say()
{
  MessageBox.Show("您好啊");
}

void Hellow()
{
  MessageBox.Show("hellow");
}

②清除监听
userName.adele = null;
③冒充监听
userName.adele(); //注意:如果清除监听后,则无法进行冒充监听

(7).既然是这样,那么如何防止清除和冒充监听呢,这里就引入了事件的概念。
第一种方法:
private adelegate _adele;
public event adelegate adele
{
  add
    {
      _adele += value;
          
    }
  
  remove
    {
      _adele -= value;
    }
}

进行调用的时候,内部
if (_adele != null)
{
   _adele();

}    外部的话,只能通过属性进行访问。因为字段是私有的,外部无法访问。

第二种方法:其实是微软给我们封装好的
public event adelegate adele;

(8).★★★★★要看事件对应的委托是什么,方法则声明为什么。

(9)小结:
委托的返回值类型,参数类型要和方法的返回值类型,参数类型一致。
委托可以直接把一个参数传递给方法,可以有返回值,也可以没有返回值。
class Program
{
    static void Main(string[] args)
    {
         List<int> list1 = new List<int>();
         list1.Add(10);
         list1.Add(20);
         list1.Add(-15);
         list1.Add(8);
         list1.Add(-20);

         adelegate adel = new adelegate(oushu);
         foreach (int i in list1)
         {
            if (adel(i))
            {
              Console.WriteLine(i);
            }
         }
            Console.ReadKey();

        }

        static bool oushu(int i)
        {
            return i > 10;
        }
    }

delegate bool adelegate(int i);

委托是一个类型,是把一个方法当做参数传递给另外一个方法。
class Program
{
   static void Main(string[] args)
  {
           List<int> list1 = new List<int>();
           list1.Add(10);
           list1.Add(20);
           list1.Add(-15);
           list1.Add(8);
           list1.Add(-20);

           List<int> newlist = list2(list1,oushu);

           foreach (int j in newlist)
           {
             Console.WriteLine(j);
           }

           Console.ReadKey();

   }

   static List<int> list2(List<int> list1,adelegate f)
    {
         List<int> list2 = new List<int>();
         foreach (int i in list1)
          {
            if (f(i))
             {
              list2.Add(i);
             }
              
          }
            return list2;

    }

   static bool oushu(int i)
     {
       return i > 10;
     }
}

delegate bool adelegate(int i);

posted @ 2011-04-18 11:04  冥海  阅读(135)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3