using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 委托实现多态
{
publicdelegate int anydelege(int x,int y);
publicdelegate anydelegeanydelegeanydelege(string s);
classProgram
{
staticvoid Main(string[]args)
{
Mathm = new Math();
anydeleges = new anydelege(m.Add);
Console.WriteLine(s(100,100));
s = newanydelege(m.Des);
Console.WriteLine(s(100,100));
anydelegeanydelegem1 = new anydelegeanydelege(m.StrAdd);
anydelegead1 = m1("add");
Console.WriteLine(ad1.Invoke(100,100));
Console.ReadKey();
}
publicclass Math
{
publicint Add(int x,int y)
{
returnx + y;
}
publicint Des(int x,int y)
{
returnx - y;
}
publicanydelege StrAdd(strings)
{
if ("add" == s.ToLower())
{
returnnew anydelege(this.Add);
}
returnnull;
}
}
}
}
委托实现多态
最新推荐文章于 2022-05-31 00:28:06 发布