C# 事件(Event) | 菜鸟教程
文章推薦指數: 80 %
C# 事件(Event) 事件(Event) 基本上说是一个用户操作,如按键、点击、鼠标移动等等,或者是一些提示信息,如系统生成的通知。
应用程序需要在事件发生时响应事件。
菜鸟教程--学的不仅是技术,更是梦想!
首页
HTML
CSS
JavaScript
Vue
Bootstrap
NodeJS
Python3
Python2
Java
C
C++
C#
Go
SQL
Linux
jQuery
本地书签
首页
HTML
CSS
JS
本地书签
Search
Python3教程
Python2教程
Vue3教程
vue2教程
Bootstrap3教程
Bootstrap4教程
Bootstrap5教程
Bootstrap2教程
C#教程
C#教程
C#简介
C#环境
C#程序结构
C#基本语法
C#数据类型
C#类型转换
C#变量
C#常量
C#运算符
C#判断
C#循环
C#封装
C#方法
C#可空类型
C#数组(Array)
C#字符串(String)
C#结构体(Struct)
C#枚举(Enum)
C#类(Class)
C#继承
C#多态性
C#运算符重载
C#接口(Interface)
C#命名空间(Namespace)
C#预处理器指令
C#正则表达式
C#异常处理
C#文件的输入与输出
C#高级教程
C#特性(Attribute)
C#反射(Reflection)
C#属性(Property)
C#索引器(Indexer)
C#委托(Delegate)
C#事件(Event)
C#集合(Collection)
C#泛型(Generic)
C#匿名方法
C#不安全代码
C#多线程
C#语言测验
C#委托(Delegate)
C#集合(Collection)
C#事件(Event)
事件(Event)基本上说是一个用户操作,如按键、点击、鼠标移动等等,或者是一些提示信息,如系统生成的通知。
应用程序需要在事件发生时响应事件。
例如,中断。
C#中使用事件机制实现线程间的通信。
通过事件使用委托
事件在类中声明且生成,且通过使用同一个类或其他类中的委托与事件处理程序关联。
包含事件的类用于发布事件。
这被称为发布器(publisher)类。
其他接受该事件的类被称为订阅器(subscriber)类。
事件使用发布-订阅(publisher-subscriber)模型。
发布器(publisher)是一个包含事件和委托定义的对象。
事件和委托之间的联系也定义在这个对象中。
发布器(publisher)类的对象调用这个事件,并通知其他的对象。
订阅器(subscriber)是一个接受事件并提供事件处理程序的对象。
在发布器(publisher)类中的委托调用订阅器(subscriber)类中的方法(事件处理程序)。
声明事件(Event)
在类的内部声明事件,首先必须声明该事件的委托类型。
例如:
publicdelegatevoidBoilerLogHandler(stringstatus);
然后,声明事件本身,使用event关键字:
//基于上面的委托定义事件
publiceventBoilerLogHandlerBoilerEventLog;
上面的代码定义了一个名为BoilerLogHandler的委托和一个名为BoilerEventLog的事件,该事件在生成的时候会调用委托。
实例
实例1
usingSystem;
namespaceSimpleEvent
{
usingSystem;
/***********发布器类***********/
publicclassEventTest
{
privateintvalue;
publicdelegatevoidNumManipulationHandler();
publiceventNumManipulationHandlerChangeNum;
protectedvirtualvoidOnNumChanged()
{
if(ChangeNum!=null)
{
ChangeNum();/*事件被触发*/
}else{
Console.WriteLine("eventnotfire");
Console.ReadKey();/*回车继续*/
}
}
publicEventTest()
{
intn=5;
SetValue(n);
}
publicvoidSetValue(intn)
{
if(value!=n)
{
value=n;
OnNumChanged();
}
}
}
/***********订阅器类***********/
publicclasssubscribEvent
{
publicvoidprintf()
{
Console.WriteLine("eventfire");
Console.ReadKey();/*回车继续*/
}
}
/***********触发***********/
publicclassMainClass
{
publicstaticvoidMain()
{
EventTeste=newEventTest();/*实例化对象,第一次没有触发事件*/
subscribEventv=newsubscribEvent();/*实例化对象*/
e.ChangeNum+=newEventTest.NumManipulationHandler(v.printf);/*注册*/
e.SetValue(7);
e.SetValue(11);
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
eventnotfire
eventfire
eventfire
本实例提供一个简单的用于热水锅炉系统故障排除的应用程序。
当维修工程师检查锅炉时,锅炉的温度和压力会随着维修工程师的备注自动记录到日志文件中。
实例2
usingSystem;
usingSystem.IO;
namespaceBoilerEventAppl
{
//boiler类
classBoiler
{
privateinttemp;
privateintpressure;
publicBoiler(intt,intp)
{
temp=t;
pressure=p;
}
publicintgetTemp()
{
returntemp;
}
publicintgetPressure()
{
returnpressure;
}
}
//事件发布器
classDelegateBoilerEvent
{
publicdelegatevoidBoilerLogHandler(stringstatus);
//基于上面的委托定义事件
publiceventBoilerLogHandlerBoilerEventLog;
publicvoidLogProcess()
{
stringremarks="O.K";
Boilerb=newBoiler(100,12);
intt=b.getTemp();
intp=b.getPressure();
if(t>150||t<80||p<12||p>15)
{
remarks="NeedMaintenance";
}
OnBoilerEventLog("LoggingInfo:\n");
OnBoilerEventLog("Temparature"+t+"\nPressure:"+p);
OnBoilerEventLog("\nMessage:"+remarks);
}
protectedvoidOnBoilerEventLog(stringmessage)
{
if(BoilerEventLog!=null)
{
BoilerEventLog(message);
}
}
}
//该类保留写入日志文件的条款
classBoilerInfoLogger
{
FileStreamfs;
StreamWritersw;
publicBoilerInfoLogger(stringfilename)
{
fs=newFileStream(filename,FileMode.Append,FileAccess.Write);
sw=newStreamWriter(fs);
}
publicvoidLogger(stringinfo)
{
sw.WriteLine(info);
}
publicvoidClose()
{
sw.Close();
fs.Close();
}
}
//事件订阅器
publicclassRecordBoilerInfo
{
staticvoidLogger(stringinfo)
{
Console.WriteLine(info);
}//endofLogger
staticvoidMain(string[]args)
{
BoilerInfoLoggerfilelog=newBoilerInfoLogger("e:\\boiler.txt");
DelegateBoilerEventboilerEvent=newDelegateBoilerEvent();
boilerEvent.BoilerEventLog+=new
DelegateBoilerEvent.BoilerLogHandler(Logger);
boilerEvent.BoilerEventLog+=new
DelegateBoilerEvent.BoilerLogHandler(filelog.Logger);
boilerEvent.LogProcess();
Console.ReadLine();
filelog.Close();
}//endofmain
}//endofRecordBoilerInfo
}
当上面的代码被编译和执行时,它会产生下列结果:
Logginginfo:
Temperature100
Pressure12
Message:O.K
C#委托(Delegate)
C#集合(Collection)
12篇笔记
写笔记
#0 qqqqqqqqqqq 264***[email protected]写一个最简单的使用事件的代码。
帮助理解。
publicclassCar
{
//这个委托用来与Car事件协作
publicdelegatevoidCarEngineHandler(stringmsg);
//这种汽车可以发送这些事件
publiceventCarEngineHandlerExploded;
publiceventCarEngineHandlerAboutToBlow;
publicintCurrentSpeed{get;set;}
publicintMaxSpeed{get;set;}
publicstringPetName{get;set;}
privateboolCarIsDead;
publicCar()
{
MaxSpeed=100;
}
publicCar(stringname,intmaxSp,intcurrSp)
{
CurrentSpeed=currSp;
MaxSpeed=maxSp;
PetName=name;
}
publicvoidAccelerate(intdelta)
{
//如果Car无法使用了,触发Exploded事件
if(CarIsDead)
{
if(Exploded!=null)
{
Exploded("sorry,thiscarisdead");
}
}
else
{
CurrentSpeed+=delta;
//确认已无法使用,触发AboutToBlow事件
if((MaxSpeed-CurrentSpeed)==10&&AboutToBlow!=null)
{
AboutToBlow("carefulbuddy!gonnablow!");
}
if(CurrentSpeed>=MaxSpeed)
{
CarIsDead=true;
}
else
{
Console.WriteLine(@"$CurrentSpeed={CurrentSpeed}");
}
}
}
}
qqqqqqqqqqq qqqqqqqqqqq 264***[email protected]年前(2018-01-23)
#0 坛子 113***[email protected]一个最简单的例子,不带参数的事件。
是实例2的一个简化版:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceBoilerEvent_tz
{
classDelegateTest
{
publicdelegatevoiddelegate_tz();
publiceventdelegate_tzdelegate_tz0;
publicvoidstart()
{
Console.WriteLine("启动事件");
delegate_tz0();//得调用该事件呀
Console.ReadKey();
}
}
classProgram
{
staticvoidMain(string[]args)
{
DelegateTestDelegateTest0=newDelegateTest();
//DelegateTest0.delegate_tz0+=DelegateTest.delegate_tz(test);//必须new一下才行,因为它是另外一个类呀
DelegateTest0.delegate_tz0+=newDelegateTest.delegate_tz(test);
DelegateTest0.start();//启动事件
}
staticpublicvoidtest()
{
Console.WriteLine("这是一个被注册的函数,按任意键继续...");
Console.ReadKey();
}
}
}
坛子 坛子 113***[email protected]年前(2018-04-08)
#0 crqq123 ays***[email protected]就第一篇笔记的具体实现:
usingSystem;
namespaceCarEvent
{
publicclassCar
{
//申明委托
publicdelegatevoidCarEngineHandler(stringmsg);
//创建委托实例Exploded和AboutToBlow事件
publiceventCarEngineHandlerExploded;
publiceventCarEngineHandlerAboutToBlow;
//设置属性
publicintCurrentSpeed{get;set;}
publicintMaxSpeed{get;set;}
publicstringPetName{get;set;}
publicboolCarIsDead;//用于判断是否超速
publicCar()//构造函数
{
MaxSpeed=100;
}
publicCar(stringname,intmaxSp,intcurrSp)//构造函数重载
{
CurrentSpeed=currSp;
MaxSpeed=maxSp;
PetName=name;
}
publicvoidAccelerate(intdelta)//用于触发Exploded和AboutToBlow事件
{
CurrentSpeed+=delta;//"踩油门"加速
if(CurrentSpeed>=MaxSpeed)//判断时速
CarIsDead=true;
else
CarIsDead=false;
if(CarIsDead)//如果Car超速了,触发Exploded事件
{
if(Exploded!=null)//判断是否被委托联系起来
{
Exploded("sorry,thiscarisdead");//调用CarDead事件
}
}
else
{//如果没有超速,则提示快要超速并显示实时车速
if((MaxSpeed-CurrentSpeed)>0&&(MaxSpeed-CurrentSpeed)<=10&&AboutToBlow!=null)//判断是否被委托联系起来且速度是否接近临界值
{
AboutToBlow("carefulbuddy!gonnablow!");//调用NearDead事件
Console.WriteLine("CurrentSpeed={0}",CurrentSpeed);//显示实时车速
}
}
}
}
//订阅类书写举例
publicclassAnswer
{
publicvoidCarDead(stringmsg)//汽车已爆缸事件
{
Console.WriteLine("sorry,thiscarisdead");
}
publicvoidNearDead(stringmsg)//汽车快要爆缸事件
{
Console.WriteLine("carefulbuddy!gonnablow!");
}
}
//主函数书写
publicclasstest
{
staticvoidMain(string[]args)
{
Carc=newCar("奔驰",100,93);//创建实例并初始化,初始速度为93
Answeran=newAnswer();
c.Exploded+=newCar.CarEngineHandler(an.CarDead);//Exploded"绑定"CarDead
c.AboutToBlow+=newCar.CarEngineHandler(an.NearDead);//AboutToBlow"绑定"NearDead
c.Accelerate(6);//第一次加速,时速小于100,引发的事件为"快要爆缸"并显示实时车速为99
Console.ReadLine();//等待回车键已启动第二次加速
c.Accelerate(2);//第二次加速,时速超过100,引发的事件为"已爆缸",不显示车速
Console.ReadKey();
}
}
}
结果为:
"carefulbuddy!gonnablow!"
"CurrentSpeed=99"
(空行)(一定要记得敲一下回车键,不然不会显示下面字符串)
"sorry,thiscarisdead"crqq123 crqq123 ays***[email protected]年前(2018-04-12)
#0 樱花树 100***[email protected]一个事件调用委托的简单例子:
usingSystem;
/*功能:当起床铃声响起,就引发学生起床/厨师做早餐两个事件
*/
//定义一个委托(也可以定义在Ring类里面)
publicdelegatevoidDoSomething();
//产生事件的类
publicclassRing
{
//声明一个委托事件
publiceventDoSomethingdoIt;
//构造函数
publicRing()
{
}
//定义一个方法,即"响铃"引发一个事件
publicvoidRaiseEvent()
{
Console.WriteLine("铃声响了.......");
//判断事件是否有调用委托(是不是要求叫学生起床,叫厨师做饭)
if(null!=doIt)
{
doIt();//如果有注册的对象,那就调用委托(叫学生起床,叫厨师做饭)
}else{
Console.WriteLine("无事发生.......");//没有注册,事件没有调用任何委托
}
}
}
//学生类(处理事件类一)
publicclassHandleEventOfStudents
{
//默认构造函数
publicHandleEventOfStudents()
{
}
//叫学生起床
publicvoidGetUp()
{
Console.WriteLine("[学生]:听到起床铃声响了,起床了。
");
}
}
//校园厨师类(处理事件类二)
publicclassHandleEventOfChefs
{
//默认构造函数
publicHandleEventOfChefs()
{
}
//叫厨师做早餐
publicvoidCook()
{
Console.WriteLine("[厨师]:听到起床铃声响了,为学生做早餐。
");
}
}
//主类
publicclassListenerEvent
{
publicstaticvoidMain(String[]args)
{
Ringring=newRing();//实例化一个铃声类[它是主角,都是因为它才牵连一系列的动作]
ring.doIt+=newHandleEventOfStudents().GetUp;//注册,学生委托铃声类,铃声响起的时候叫我起床.
ring.doIt+=newHandleEventOfChefs().Cook;//注册,厨师告诉铃声类,我也委托你叫我什么时候做早餐
ring.RaiseEvent();//铃声响起来了,它发现学生和厨师都拜托(注册)了自己,然后它就开始叫学生起床,叫厨师做早餐(一个事件调用了两个委托)
}
}
你可能发现,上面的注册代码和前面的例子都不一样。
这是因为ring.doIt本来就是DoSomething类型的,C#会自动把学生类方法转换成相同的类型(猜测,但是上面的代码可以完美运行)。
当然上面的注册代码也可以写成和文章例子的一样。
改成这样ring.doIt+=newRing.DoSomething(newHandleEventOfStudents().GetUp);这样也可以实现,当然这样的话定义委托的语句就要写在Ring类里面了。
樱花树 樱花树 100***[email protected]年前(2018-06-11)
#0 dzb duz***[email protected]相当于定义一个函数类型。
event相当于定义一个delegate的函数指针(回调函数指针)。
这样就好理解了。
dzb dzb duz***[email protected]年前(2018-08-17)
#0 Heavy 178***[email protected]实例:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceBoilerEvent_tz
{
classDelegateTest//发布器类
{
publicdelegatevoiddelegate_tz();
publiceventdelegate_tzdelegate_tz0;
publicstaticvoidget()
{
Console.WriteLine("这是触发事件的第一个方法,此方法在事件类中");
}
publicvoidinter()
{
Console.WriteLine("开始调用事件方法");
delegate_tz0();
}
}
classTest//订阅器类
{
publicTest()
{}
publicvoidTryEvent()
{
Console.WriteLine("这是触发事件的第三个方法,在订阅器中,这才是正宗的订阅器类");
Console.ReadKey();
}
}
classProgram//主体函数程序入口
{
staticvoidMain(string[]args)
{
DelegateTestDelegateTest0=newDelegateTest();
//主体函数中根据需求组装事件,组装过程类似委托多播
DelegateTest0.delegate_tz0+=newDelegateTest.delegate_tz(DelegateTest.get);
DelegateTest0.delegate_tz0+=newDelegateTest.delegate_tz(Method1);
DelegateTest0.delegate_tz0+=newDelegateTest.delegate_tz(newTest().TryEvent);
DelegateTest0.inter();//执行操作,以触发事件
}
staticpublicvoidMethod1()
{
Console.WriteLine("这是触发事件的第二个方法,在主体Main函数中");
}
}
}
执行输出结果为:
开始调用事件方法
这是触发事件的第一个方法,此方法在事件类中
这是触发事件的第二个方法,在主体Main函数中
这是触发事件的第三个方法,在订阅器中,这才是正宗的订阅器类
Heavy Heavy 178***[email protected]年前(2018-12-19)
#0 bai 229***[email protected]事件的运用总结
事件的整个过程是:订阅->发布->执行。
订阅:假设事件A的执行方法是F_A,事件B的执行方法是F_B,将这些事件与它们的委托人进行绑的行为就是订阅,这个委托人就是发布器的一个成员。
订阅器另一个行为就是在订阅之后(必学先订阅)通知发布器的相关成员。
发布:首先要明确事件发布的类型(由事件的执行方法参数列表决定)和要发布事件的变量(这个变量即委托人);其次整理发布所需的材料、判断条件是否合适等;最后让内部的委托人向执行函数传递最终信息。
执行:是整个事件最后完成的步骤,就是普通的函数。
bai bai 229***[email protected]年前(2019-01-28)
#0 咚伊夏 416***[email protected]明明可以很简单(对实例1的简单修改):
usingSystem;
namespaceSimpleEvent
{
/***********发布器类***********/
publicclassEventTest
{
publicdelegatevoidNumManipulationHandler();//声明委托
publiceventNumManipulationHandlerChangeNum;//声明事件
publicvoidOpenDoor()
{
ChangeNum();//事件触发
}
}
/***********订阅器类***********/
publicclasssubscribEvent
{
publicvoidprintf()
{
Console.WriteLine("Thedoorisopened.");
}
}
/***********触发***********/
publicclassMainClass
{
publicstaticvoidMain()
{
EventTeste=newEventTest();/*实例化事件触发对象*/
subscribEventv=newsubscribEvent();/*实例化订阅事件对象*/
/*订阅器的printf()在事件触发对象中注册到委托事件中*/
e.ChangeNum+=newEventTest.NumManipulationHandler(v.printf);
e.OpenDoor();/*触发了事件*/
}
}
}咚伊夏 咚伊夏 416***[email protected]年前(2019-02-13)
#0 zy 353***[email protected]一个很简单的例子:当宠物店有新狗时通知订阅客户。
usingSystem;
namespaceBoilerEvent
{
classDog
{
privateStringname;
privateStringcolor;
publicdelegatevoidHandeler();//定义委托
publicstaticeventHandelerNewDog;//定义事件
publicDog(StringName,StringColor)
{
name=Name;
color=Color;
if(NewDog!=null)
{
Console.WriteLine("新进了一只狗");
NewDog();//调用事件
}
}
}
classTest
{
publicstaticvoidMain(String[]args)
{
Dogd=newDog("Tony","yellow");//因为还没有添加订阅,所以不能触发事件
Dog.NewDog+=newDog.Handeler(Client1);//Client1添加订阅
Dog.NewDog+=newDog.Handeler(Client2);//Client2添加订阅
Dogd2=newDog("Tom","white");
}
staticvoidClient1()
{
Console.WriteLine("我喜欢这条狗!");
}
staticvoidClient2()
{
Console.WriteLine("我非常想要!");
}
}
}zy zy 353***[email protected]年前(2019-07-26)
#0 Oldyoung yya***[email protected]#这种事件—响应类比Python的信号和槽机制。
同时,事件—响应的参数传递是默认的。
委托定义好事件和响应函数的输入输出类型,然后事件的参数会自动传递给响应函数。
usingSystem;
/***********发布器类***********/
publicclassEventTest
{
publicdelegatevoidNumManipulationHandler(stringname,inta);//声明委托
publiceventNumManipulationHandlerChangeNum;//声明事件
publicvoidOpenDoor(stringname,inta)//模拟事件
{
ChangeNum(name,a);//事件触发
}
}
/***********订阅器类***********/
publicclasssubscribEvent
{
publicvoidresult(stringname,inta)
{
Console.WriteLine("{0}isarrivingatAirportTerminal{1}",name,a);
}
}
/***********触发***********/
publicclassMainClass
{
publicstaticvoidMain()
{
EventTeste=newEventTest();/*实例化事件触发对象*/
subscribEventv=newsubscribEvent();/*实例化订阅事件对象*/
/*订阅器的printf()在事件触发对象中注册到委托事件中*/
e.ChangeNum+=newEventTest.NumManipulationHandler(v.result);
e.OpenDoor("Yang",3);/*模拟事件*/
}
}Oldyoung Oldyoung yya***[email protected]年前(2019-07-26)
#0 mmcike 271***[email protected]事件是拥有可以注册和解绑方法(函数)的功能。
虽然事件和委托看起来有点绕,只要捋清楚事件和委托的关系,就会很容易理解。
委托是一个类,事件则是委托类中的一个对象,该对象是能够把其他方法注册到委托类中的一个事件(如果觉得有点绕,可以忽略这句话)。
事件和函数的关系:事件具有可以注册多个函数(和解绑函数)的功能,而函数如果要注册和解绑其他在其主体上运行的函数则需要改动该函数本体的代码,这就是区别。
以下代码的大致流程:定义一个新类(事件类)--》类中声明委托--》由委托类又声明事件--》再定义触发事件的函数--》函数主体中执行事件--》在主函数中实例化事件类--》进而调用事件类中的事件对象--》事件对象再注册(+=)两个方法--》再执行事件类中触发事件的那个函数--》再解绑其中一个方法--》再次执行事件类中触发事件的函数。
由此可见:事件是拥有可以注册和解绑方法(函数)的功能。
usingSystem;
namespaceDelegateAndEvent
{
//定义一个事件类
publicclassMyEvent
{
//定义一个委托
publicdelegatevoidMyDelegate();
//定义一个事件
publicMyDelegateMyDelegateEvent;
//定义一个触发事件的函数
publicvoidOnMyDelegateEvent()
{
//判断事件是否非空
if(MyDelegateEvent!=null)
{
//执行事件
MyDelegateEvent();
}
//MyDelegateEvent?.Invoke();//简化的判断和执行
}
}
classProgram
{
//输出一串字符
publicstaticvoidputOutChar()
{
Console.WriteLine("Iwasfired");
}
//输出第二串字符
publicstaticvoidputOutChar2()
{
Console.WriteLine("Iwasfired22222");
}
staticvoidMain(string[]args)
{
//实例化MyEvent2类
MyEventmyEvent=newMyEvent();
//注册一个事件
myEvent.MyDelegateEvent+=newMyEvent.MyDelegate(putOutChar);
myEvent.MyDelegateEvent+=newMyEvent.MyDelegate(putOutChar2);
//执行触发事件的函数
Console.WriteLine("执行绑定了两个事件后的函数");
myEvent.OnMyDelegateEvent();
//解绑一个事件
myEvent.MyDelegateEvent-=newMyEvent.MyDelegate(putOutChar);
//再次执行触发事件的函数
Console.WriteLine("执行解绑了一个事件后的函数");
myEvent.OnMyDelegateEvent();
Console.ReadKey();
}
}
}mmcike mmcike 271***[email protected]年前(2019-08-28)
#0 cedar_forest 190***[email protected]行简单例子:女朋友要求男朋友做的事。
usingSystem;
namespacePractice
{
publicdelegatevoidJob();//任务列表委托
classShe{
privateeventJobBoyFriendJobs=null;//男朋友任务列表
publicvoidcommand(){
if(BoyFriendJobs!=null){//有男朋友的时候,发号施令
BoyFriendJobs();
}else{
Console.WriteLine("原来我还没有男朋友啊...");
}
}
publicvoidaddJob(Jobjob){//动态添加男朋友的任务,注意传入一个Job类型的方法
BoyFriendJobs+=job;
}
}
classHe{
publicvoidbuyTrainTicket(){//男朋友的任务
Console.WriteLine("买车票");
}
publicvoidbuyCinemaTicket(){
Console.WriteLine("买电影票");
}
publicvoidbuyFood(){
Console.WriteLine("买吃的");
}
}
classProgram
{
publicstaticvoidMain(string[]args){
Sheshe=newShe();
Hehe=newHe();
she.addJob(he.buyTrainTicket);//添加男朋友任务
she.addJob(he.buyFood);
she.addJob(he.buyCinemaTicket);
she.command();
}
}
}
输出:
买车票
买吃的
买电影票cedar_forest cedar_forest 190***[email protected]年前(2020-08-30)
点我分享笔记
取消
分享笔记
昵称昵称(必填)
邮箱邮箱(必填)
引用地址引用地址
分类导航
HTML/CSSHTML教程HTML5教程CSS教程CSS3教程Bootstrap3教程Bootstrap4教程Bootstrap5教程FontAwesome教程Foundation教程JavaScriptJavaScript教程HTMLDOM教程jQuery教程AngularJS教程AngularJS2教程Vue.js教程Vue3教程React教程TypeScript教程jQueryUI教程jQueryEasyUI教程Node.js教程AJAX教程JSON教程Echarts教程Highcharts教程Google地图教程服务端Python教程Python2.x教程Linux教程Docker教程Ruby教程Java教程C教程C++教程Perl教程Servlet教程JSP教程Lua教程Rust教程Scala教程Go教程PHP教程Django教程Zookeeper教程设计模式正则表达式Maven教程Verilog教程ASP教程AppML教程VBScript教程数据库SQL教程MySQL教程PostgreSQL教程SQLite教程MongoDB教程Redis教程Memcached教程数据分析Python教程NumPy教程Pandas教程Matplotlib教程Scipy教程R教程Julia教程移动端Android教程Swift教程jQueryMobile教程ionic教程Kotlin教程XML教程XML教程DTD教程XMLDOM教程XSLT教程XPath教程XQuery教程XLink教程XPointer教程XMLSchema教程XSL-FO教程SVG教程ASP.NETASP.NET教程C#教程WebPages教程Razor教程MVC教程WebForms教程WebServiceWebService教程WSDL教程SOAP教程RSS教程RDF教程开发工具Eclipse教程Git教程Svn教程Markdown教程网站建设HTTP教程网站建设指南浏览器信息网站主机教程TCP/IP教程W3C教程网站品质
Advertisement
反馈/建议
在线实例
·HTML实例
·CSS实例
·JavaScript实例
·Ajax实例
·jQuery实例
·XML实例
·Java实例
字符集&工具
·HTML字符集设置
·HTMLASCII字符集
·JS混淆/加密
·PNG/JPEG图片压缩
·HTML拾色器
·JSON格式化工具
·随机数生成器
最新更新
·
JavaScript类(c...
·
JavaScript类(c...
·
JavaScript类(c...
·
JavaScript类(c...
·
CSSminmax()函数
·
CSSrepeat()函数
·
JavaScript判断...
站点信息
·
意见反馈
·
免责声明
·
关于我们
·
文章归档
关注微信
Copyright©2013-2022菜鸟教程
runoob.comAllRightsReserved.备案号:闽ICP备15012807号-1
微信关注
延伸文章資訊
- 1[C#語法] 事件(Event)使用簡單三步| 誠的碼記本 - - 點部落
用C#自訂事件簡單作法. ... 委派 delegate void EventPrototype(object sender, EventArgs e); //宣告事件 public event...
- 2C#事件:Event - C语言中文网
在C# 语言中,Windows 应用程序、 ASP.NET 网站程序等类型的程序都离不开事件的应用。 事件是一种引用类型,实际上也是一种特殊的委托。 通常,每一个事件的发生都会 ...
- 3處理和引發事件
事件與EventHandler 委派有關,而且會在名為 OnThresholdReached 的方法中引發。 C# 複製. class Counter { ...
- 4C# 事件(上) - 使用委派來實作事件 - iT 邦幫忙
C#的事件我在ASP.NET MVC中印像中是沒有看過,envent應該是在視窗程式或WebForm上才會看到?。不過記得以前寫WebForm的過程中,雖然每個控制項都會用到事件,但記得我 ...
- 5C# 事件(Event) | 菜鸟教程
C# 事件(Event) 事件(Event) 基本上说是一个用户操作,如按键、点击、鼠标移动等等,或者是一些提示信息,如系统生成的通知。应用程序需要在事件发生时响应事件。