需求:2018年Apple的MacBookPro新电脑已经出来了,这年的电脑增加了许多亮点,比如RAM可配置成32G,小A看到后,欣喜若狂,于是,亦然决然第要要买一台。
1、用委托的方式购买。
由于Apple公司是在美国,为了买个MacBookPro难道还要跑到美国吗(这样加上路费成本会高很多,如果专门跑到美国购买,太不划算了)?不用专门跑到美国买,现在可以通过苹果指定实体店、电商平台购买和官方旗舰店购买。于是乎,小A决定在Apple的官方旗舰店上订购一台。
声明委托:
//声明一个委托购买MacBookPro的委托。
private delegate void BuyMacBookPro();
声明委托执行的方法:
//电商平台(委托要执行的方法)。
private static void BuyMacBookProCallBack() { Console.WriteLine("我是Apple官方旗舰店,恭喜你成功订购了一台MacBookPro!"); }
将委托与方法建立联系:
static void Main(string[] args) { //小A和Apple官方旗舰店建立联系(给委托指定方法)。
BuyMacBookPro buyMacBookPro = new BuyMacBookPro(BuyMacBookProCallBack); //小A给Apple官方旗舰店打钱,Apple官方旗舰店给小A发电脑(触发)。
buyMacBookPro(); }
2、用Action的方式购买。
9月份即将到来,一年一度的IPhone又该与大家见面了,小A是个数码控,于是辛辛苦苦吃土3个月决定买一个IPhone。经过购买MacBookPro的经验来讲,每次购买都要声明一个委托,有没有什么好一点的办法买IPhone时不用定义委托?于是乎,可以使用Action,只需要传入你要购买的手机型号即可。
声明Action要执行的方法:
private static void BuyIPhoneCallBack(string _name) { Console.WriteLine("我是Apple官方旗舰店,恭喜你成功订购了一台{0}", _name); }
声明Action并触发:
static void Main(string[] args) { //实例化Action,为其指定要执行的函数。
Action<string> action = new Action<string>(BuyIPhoneCallBack); //触发。
action("IPhone9"); }
3、Func。
Func:封装一个不具有参数(也可有参数)但却返回TResult参数指定的类型的值的方法。
3.1)、没有参数的Func。
static void Main(string[] args) { //封装一个不具有参数(也可有参数)但却返回TResult参数指定的类型的值的方法。 //string:指返回值类型。
Func<string> func = new Func<string>(FuncCallBack); string result = func(); Console.WriteLine(result); Console.ReadLine(); } private static string FuncCallBack() { return "OK"; }
结果为 "OK"。
3.2)、有参数的Func。
static void Main(string[] args) { //有5个int类型的参数,返回值是int类型。
Func<int, int, int, int, int, int> func = new Func<int, int, int, int, int, int>(SumCallBack); //触发,填入参数。
int result = func(1, 2, 3, 4, 5); Console.WriteLine(result); Console.ReadLine(); } private static int SumCallBack(int arg1, int arg2, int arg3, int arg4, int arg5) { return arg1 + arg2 + arg3 + arg4 + arg5; }
结果为15
3.3)、用Func传递值。
static void Main(string[] args) { //还可以简写成: //Func<string> func = delegate { return "OK"; };/ Func<string> func = () => { return "OK"; };
Func<string> func = new Func<string>(delegate { return "我是通过Func的方式传递值"; }); DisplayFuncValue(func); Console.ReadLine(); } private static void DisplayFuncValue(Func<string> _func) { string result = _func(); Console.WriteLine(result); }
结果为:
4、什么时候用Action,什么时候用Func呢?
Action用于没有返回值的方法(参数可以根据自己情况进行传递)。
Func恰恰相反用于有返回值的方法(同样参数根据自己情况情况)。
记住无返回就用action,有返回就用Func。
5、Task。
task中文意思是工作、任务的意思。
task:表示一个可以返回值的异步操作。
5.1)、创建Task。
创建Task有两种方式:分别是实例化Task对象和通过Factory.StartNew()方法。
a)、实例化对象的方法创建Task。
static void Main(string[] args) { //声明Action类型的委托。
Action action = new Action(ActionCallBack); //实例化Task对象,传入一个委托。
Task task = new Task(action); //启动 System.Threading.Tasks.Task,并将它安排到当前的 System.Threading.Tasks.TaskScheduler中。
task.Start(); Console.ReadLine(); } private static void ActionCallBack() { Console.WriteLine("我是Action委托执行的方法。"); }
b)、通过Factory.StartNew()。
static void Main(string[] args) { //声明Action类型的委托。
Action action = new Action(ActionCallBack); Task.Factory.StartNew(action); Console.ReadLine(); } private static void ActionCallBack() { Console.WriteLine("我是Action委托执行的方法。"); }
5.2)、Result属性。
为其传入泛型参数,指定返回类型,通过Result属性可获取返回值。
static void Main(string[] args) { //指定返回类型为int类型。
Task<int> task = new Task<int>(TestMethod); task.Start(); //当走到task.Result时,进入TestMethod方法,Result属性适合得到有返回值的函数。
int result = task.Result; Console.WriteLine(result); Console.ReadLine(); } private static int TestMethod() { return 12345; }
返回结果为:12345
5.3)、为Task传入参数Func类型的参数。
static void Main(string[] args) { //也可写成:Func<string> func = () => { return "OK"; };
Func<string> func = new Func<string>(TestCallBack); //实例化Task对象,为其传入Func类型的参数。
Task<string> task = new Task<string>(func); task.Start(); Console.WriteLine(task.Result); Console.ReadLine(); } private static string TestCallBack() { return "OK"; }
6、线程同步(lock(this))。
class Program { static void Main(string[] args) { SaleBook sb = new SaleBook(); Thread th1 = new Thread(new ParameterizedThreadStart(sb.MaiShu)); Thread th2 = new Thread(new ParameterizedThreadStart(sb.MaiShu)); th1.Start("线程1"); th2.Start("线程2"); Console.ReadKey(); } } public class SaleBook { int bookCount = 1; public void MaiShu(object _threadName) { Console.WriteLine(string.Format("名叫{0}的线程调用了此方法", _threadName.ToString())); lock (this) { int tmp = bookCount; if (tmp > 0) //判断是否有书,如果有就可以卖
{ Thread.Sleep(1000); bookCount = bookCount - 1; Console.WriteLine("刚售出一本书,还剩{0}本", bookCount); } else { Console.WriteLine("不好意思,书已卖完,欢迎下次光临!"); } } } }
参考博客链接:https://www.cnblogs.com/LipeiNet/p/4694225.html
End!