1.委托(Action,Func)
using System;
namespace bDelegate
{
//委托-----------------------------------------------------
//别名:句柄、代理
//自定义类型、引用类型
//就是来存储方法的结构
//delegate 返回值类型 委托类型名(参数列表);
//委托对象-------------------------------------------------
//[访问修饰符] 委托类型名称 委托对象的名称;
//给委托对象赋值
//委托对象 = 方法名;
//xiaoming.buyHoueEventHandle = intermediary.BuyHouse;(注意只写方法名,没有括号)
//ips:委托对象,保存的是方法的内存地址
//public BuyHouseEventHandle buyHouseEventHandle;
//委托对象的绑定与解绑定
//xiaoming.buyHoueEventHandle += lianjia.BuyHouse;
//xiaoming.buyHoueEventHandle -= lianjia.BuyHouse;
//委托对象的执行
//委托对象(参数列表);
//House house = xiaoming.buyHoueEventHandle(1000000,80);
//Tips:委托对象为空是不能执行的,执行前先提前判断
public class ActionAndFunc
{
//无返回值无参
private Action testAction;
//无返回值有参
private Action<float> floatAction;
private Action<float, int> floatIntAction;
//有返回值有参
private Func<int> returnIntFunc;
private Func<int, float> newFunc;
void Bind()
{
Action<int, float, string> test1Action = Test1;
Func<int, Apartment> test2Func = Test2;
test2Func += Test2;
test2Func = Test2;
}
Apartment Test2(int money)
{
return null;
}
void Test1(int a, float b, string c)
{
}
}
}
2.匿名函数(普通方式,拉姆达表达式)
using System;
namespace bDelegate
{
//匿名函数
//匿名函数就是没有函数名的函数
//匿名函数仅限委托绑定
public class Hero
{
private byte age;
public string name;
public Action<Hero> attack;
public Hero(string name)
{
this.name = name;
}
public byte Age
{
get => age;
set => age = value;
}
}
public class AnonymousMethod
{
public static void Demo()
{
Hero akl = new Hero("阿卡丽");
akl.attack = TestAttack;
//匿名方法绑定
akl.attack += delegate(Hero hero)
{
Console.WriteLine(hero.name + "正在被攻击...");
};
//拉姆达表达式的方式绑定
akl.attack += hero =>
{
Console.WriteLine(hero.name + "正在被攻击...");
};
akl.attack(new Hero("提莫"));
}
public static void TestAttack(Hero hero)
{
Console.WriteLine(hero.name + "正在被攻击...");
}
}
}
3.回调
using System;
namespace bDelegate
{
//回调
public class CallbackDemo
{
public void Doit()
{
PrintNumbers(SayHello);
}
//Action aGoodThing = SayHello;
private void PrintNumbers(Action aGoodThing)
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine(i + 1);
}
aGoodThing();
}
private void SayHello()
{
Console.WriteLine("Hello");
}
}
public class Monster
{
public void TakeDamage(float damage)
{
}
}
}
4.事件
using System;
namespace bDelegate
{
//事件
//特殊的委托对象
//在非事件所在的类中,只能对事件进行绑定(+=)和解绑定(-=)
//在事件所在的类中,可以对事件进行赋值(=)、调用、绑定(+=)和解绑定(-=)
public delegate void MyEventHandle(float a, int b);
public class OtherClass
{
public void Test()
{
EventDemo demo = new EventDemo();
demo._myEventHandle += ShowNumbers;
demo._myEventHandle -= demo.MyFunction;
}
public void ShowNumbers(float a, int b)
{
}
}
public class EventDemo
{
// 委托类型 委托对象
public event MyEventHandle _myEventHandle;
void Demo()
{
_myEventHandle = (f, i) => { };
_myEventHandle += (f, i) =>
{
Console.WriteLine("Hello");
};
_myEventHandle = MyFunction;
}
public void MyFunction(float a, int b)
{
}
}
}
4.作业
创建⽼板类,设计字典存储⽼板的所有资源
资源内容包括:
潘⽯屹:可以提供免费的办公场所
⻢云:可以提供低价的货源
雷军:可以提供销售能⼒极强的员⼯
通过委托回调建⽴这些联系
完成⽼板开⼯卖货的功能
using System;
using System.Collections.Generic;
namespace test1
{
class Boss
{
public delegate void SaleGoods();
public static void Main(string[] args)
{
SaleGoods test = new SaleGoods(Resouce.FreeRoom);
test += Resouce.LowerPriceGood;
test += Resouce.SaleStaff;
test();
}
}
public class Resouce
{
public static void FreeRoom()
{
Console.WriteLine("提供了免费的办公场所...");
}
public static void LowerPriceGood()
{
Console.WriteLine("提供了低价的货物...");
}
public static void SaleStaff()
{
Console.WriteLine("提供了销售能力极强的工作人员...");
}
}
}