关键字delegate
delegate void Name()
Name mikal
mikal。。
void类型并且没有返回值
委托---》数据类型分值类型和引用类型
2。申明委托变量 委托类型 委托变量
3.委托变量初始化:委托变量名=new 委托类型名(委托的方法)
委托---函数的指针分方法和引用
public void mikal()
{
//mikal=new mikal(folder.mikal);这是一种
mikal+=folder.mikal;//另外一种
}
委托:是一种数据类型,声明是关键字加类型名delegate void Name(); ,是值类型还是引用类型、委托是引用类型与类是一种类型,声明委托变量用委托类型名声明,委托变量的初始化、实例化 :委托变量名=new 委托类型名(委托的方法);就是函数的指针,是一个方法的引用
多播委托 :指向多个方法叫多播委托 ,从而实现委托连。
delegate void stopManchineryDelegat();//定义委托stopManchineryDelegat//void类型并且没有返回值。
private stopManchineryDelegat stopManchinery;//将委托具体化--stopManchinery
实例:
static void Main(string[] args)
{
FoldingMachine folder = new FoldingMachine();
WeldingMachine welder = new WeldingMachine();
PaintingMachine painer=new PaintingMachine();
Controller controller = new Controller();
controller.Folder = folder;
controller.Welder = welder;
controller.Painer = painer;
controller.ShutDown();
//controller.Add(welder)
Person p=new Person(20,"tom");
p.speak();
Console.ReadKey();
}
private FoldingMachine folder;
private WeldingMachine welder;
private PaintingMachine painer;
delegate void stopManchineryDelegat();//定义委托stopManchineryDelegat//void类型并且没有返回值。
private stopManchineryDelegat stopManchinery;//将委托具体化--stopManchinery
//public stopManchineryDelegat StopManchinery
//{
// get { return stopManchinery; }
// set { stopManchinery = value; }
//}
//public void Add(stopManchineryDelegat stopMethod)
//{
// stopManchinery += stopMethod;
//}
public void SetStopManchinery()
{
stopManchinery += folder.StopFolding;//一种
stopManchinery += welder.WeldingFolding;
stopManchinery += painer.PainOff;
//stopManchinery = new stopManchineryDelegat(folder.StopFolding);
}
public FoldingMachine Folder
{
set { this.folder = value; }
}
public WeldingMachine Welder
{
set { this.welder = value; }
}
public PaintingMachine Painer
{
set { this.painer = value; }
}
public void ShutDown()
{
folder.StopFolding();
welder.WeldingFolding();
painer.PainOff();
}
public void WeldingFolding()
{
Console.WriteLine("折叠机停止工作");
}