怎样用自己的电脑,做网站,网页版传奇手游,网店logo设计图片免费,wap网站搭建很多小伙伴学习C# 的委托时往往一头雾水, 不明白委托是什么, 有什么作用, 今天我就用2分钟讲清楚 
这是一个C# 的控制台程序 定义一个最简单的委托 delegate int Calculate(int a, int b); 
这相当于定义了一个Calculate类型, 只不过这个类型需要传入2个int类型的参数 返回值也…很多小伙伴学习C# 的委托时往往一头雾水, 不明白委托是什么, 有什么作用, 今天我就用2分钟讲清楚 
这是一个C# 的控制台程序 定义一个最简单的委托 delegate int Calculate(int a, int b); 
这相当于定义了一个Calculate类型, 只不过这个类型需要传入2个int类型的参数 返回值也是int 
委托的意义在于, 它可以将函数封装成可以像变量 int, string这样的类型传入到另外的函数中, 例如函数int Test(int a, int b, Calculate c) 这大大提高了函数的功能和可扩展性, 可以在不改变Test函数的参数的同时, 通过改变Calculate 来改变Test内部的逻辑关系 
namespace DelegateTest
{public class Program{//定义一个Calculate 类型的委托delegate int Calculate(int a, int b); static void Main(string[] args){//定义一个返回值和参数都与Calculate相同的加法函数int Add(int a, int b){return a  b;}//定义一个返回值和参数都与Calculate相同的减法函数int Sub(int a, int b){return a - b;}//将委托类型calculate当参数传入函数int Test(int a, int b, Calculate calculate){return calculate(a, b)  a;}//委托的精髓在于, 将委托看作一种类型, 和int, String一样的类型.//只不过int a  b; 是将 int类型的变量 b 赋值给 a, //而Calculate calculate1  Sub; 是将Calculate类型的 Sub 赋值给 calculate1Calculate calculate1  Sub; //定义一个Calculate 类型的calculate1字段, 并将Sub函数传递给它Calculate calculate2  Add; //定义一个Calculate 类型的calculate2字段, 并将Add函数传递给它int i  Test(1, 2, calculate1); //返回 1  (12)  4int j  Test(1, 2, calculate2); //返回 1  (1-2)  0Console.WriteLine(i的值是:  i);Console.WriteLine(j的值是:  j);}}}很多人不知道, C# 的委托, 和C语言的函数指针是一个东西 
我们把C sharp的 delegate int Calculate(int a, int b) 换成C语言的 typedef int (*Calculate)(int a, int b) 
// FunctionPoint.cpp : 此文件包含 main 函数。程序执行将在此处开始并结束。
//#include stdio.h//定义一个Calculate 类型的委托//delegate int Calculate(int a, int b); typedef int (*Calculate)(int a, int b);//定义一个返回值和参数都与Calculate相同的加法函数int Add(int a, int b){return a  b;}//定义一个返回值和参数都与Calculate相同的减法函数int Sub(int a, int b){return a - b;}//将委托类型calculate当参数传入函数int Test(int a, int b, Calculate calculate){return a  calculate(a, b);}int main()
{//委托/函数指针的精髓在于, 将委托看作一种类型, 和int, String一样的类型.//只不过int a  b; 是将 int类型的变量 b 赋值给 a, //而Calculate calculate1  Sub; 是将Calculate类型的 Sub 赋值给 calculate1Calculate calculate1  Add;Calculate calculate2  Sub;int i  Test(1, 2, calculate1); //返回 1  (12)  4int j  Test(1, 2, calculate2); //返回 1  (1-2)  0printf(i的值是: %d\n, i);printf(j的值是: %d\n, j);
} 
可以运行一下, 这两个程序返回的结果是一样的. 如果放在java 上怎么实现? 
函数式接口和委托, 函数指针是一脉相承的! 
package com.example.design;import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;SpringBootTest
class DesignApplicationTests {//定义一个Calculate 类型的委托//delegate int Calculate(int a, int b);//typedef int (*Calculate)(int a, int b);FunctionalInterfaceinterface ICalculate{Integer Calculate (Integer a, Integer b);}//定义一个返回值和参数都与Calculate相同的加法函数Integer Add(Integer a, Integer b){return a  b;}//定义一个返回值和参数都与Calculate相同的减法函数Integer Sub(Integer a, Integer b){return a - b;}//将委托类型calculate当参数传入函数Integer Test(Integer a, Integer b, ICalculate iCalculate){return a  iCalculate.Calculate(a, b);}Testvoid contextLoads() {//委托/函数指针的精髓在于, 将委托看作一种类型, 和int, String一样的类型.//只不过int a  b; 是将 int类型的变量 b 赋值给 a,//而Calculate calculate1  Sub; 是将Calculate类型的 Sub 赋值给 calculate1ICalculate calculate1  this::Add;ICalculate calculate2  this::Sub;Integer i  Test(1,2,calculate1);//返回 1  (12)  4Integer j  Test(1,2,calculate2);//返回 1  (1-2)  0System.out.println(i的值是:i);System.out.println(j的值是:j);}} 文章转载自: http://www.morning.yjknk.cn.gov.cn.yjknk.cn http://www.morning.qrwdg.cn.gov.cn.qrwdg.cn http://www.morning.lfjmp.cn.gov.cn.lfjmp.cn http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn http://www.morning.yqhdy.cn.gov.cn.yqhdy.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.tbbxn.cn.gov.cn.tbbxn.cn http://www.morning.nwgkk.cn.gov.cn.nwgkk.cn http://www.morning.jnrry.cn.gov.cn.jnrry.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.lynkz.cn.gov.cn.lynkz.cn http://www.morning.xczyj.cn.gov.cn.xczyj.cn http://www.morning.dfndz.cn.gov.cn.dfndz.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.sxfmg.cn.gov.cn.sxfmg.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.ryrpq.cn.gov.cn.ryrpq.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.dyzbt.cn.gov.cn.dyzbt.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.cbynh.cn.gov.cn.cbynh.cn http://www.morning.wmpw.cn.gov.cn.wmpw.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.bsqth.cn.gov.cn.bsqth.cn http://www.morning.pfkrw.cn.gov.cn.pfkrw.cn http://www.morning.qfplp.cn.gov.cn.qfplp.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.grtwn.cn.gov.cn.grtwn.cn http://www.morning.pndhh.cn.gov.cn.pndhh.cn http://www.morning.rcqyk.cn.gov.cn.rcqyk.cn http://www.morning.kqxng.cn.gov.cn.kqxng.cn http://www.morning.gmwqd.cn.gov.cn.gmwqd.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.nqmkr.cn.gov.cn.nqmkr.cn http://www.morning.piekr.com.gov.cn.piekr.com http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.nxdqz.cn.gov.cn.nxdqz.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.lxjxl.cn.gov.cn.lxjxl.cn http://www.morning.dtlqc.cn.gov.cn.dtlqc.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.trjp.cn.gov.cn.trjp.cn http://www.morning.qlhwy.cn.gov.cn.qlhwy.cn http://www.morning.yqwsd.cn.gov.cn.yqwsd.cn http://www.morning.glnfn.cn.gov.cn.glnfn.cn http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.rbxsk.cn.gov.cn.rbxsk.cn http://www.morning.mlfmj.cn.gov.cn.mlfmj.cn http://www.morning.qdbcd.cn.gov.cn.qdbcd.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.ngcth.cn.gov.cn.ngcth.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.wqkzf.cn.gov.cn.wqkzf.cn http://www.morning.fzqfb.cn.gov.cn.fzqfb.cn http://www.morning.nflpk.cn.gov.cn.nflpk.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.csnmd.cn.gov.cn.csnmd.cn http://www.morning.tntqr.cn.gov.cn.tntqr.cn http://www.morning.rpwm.cn.gov.cn.rpwm.cn http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.rshs.cn.gov.cn.rshs.cn