using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
#region 事件
public delegate void mydelegate();
/// <summary>
/// 事件处理方法类
/// </summary>
class MyEventHander
{
public void OnHandler1()
{
Console.WriteLine("调用 OnHandler1() 方法!!");
}
}
/// <summary>
/// 事件声明、触发该事件的方法
/// </summary>
class MyEvent
{
public event mydelegate Event1;
public void FireEvent1()
{
if (Event1 != null)
{
Event1();
}
}
}
class Program
{
static void Main(string[] args)
{
#region 淡定
//int tagID = 6495;
//byte b1,b2;
//b1 = (byte)(tagID >> 8);
//b2 = (byte)tagID;
//System.Console.WriteLine("b1:{0:X2},b2:{1:X2}", b1, b2);
#endregion
MyEvent b = new MyEvent();//实例化对象,b为 事件类 对象名
MyEventHander a = new MyEventHander();//实例化对象,a为 事件处理方法类 对象名
b.Event1 += new mydelegate(a.OnHandler1);//将事件处理方法绑定到事件
b.FireEvent1();//事件触发方法
}
}
#endregion
#region 事件
public delegate void mydelegate(int c, int n);//声明一个事件委托
/// <summary>
/// 事件声明、事件触发方法类
/// </summary>
public class Shape
{
protected int color = 0;
public event mydelegate ColorChange;//声明一个事件
public int pcolor//定义属性
{
set
{
int ocolor = color;
color = value;
ColorChange(ocolor, color);
}
get { return color; }
}
//public Shape()//构造函数
//{
// color = 0;
//}
}
/// <summary>
/// 事件处理方法类
/// </summary>
public class MyEventHander
{
public void EentHander(int c, int n)
{
Console.WriteLine("事件处理方法!");
}
}
class Program
{
static void Main(string[] args)
{
Shape obj = new Shape();
MyEventHander obj1 = new MyEventHander();
obj.ColorChange += new mydelegate(obj1.EentHander);
}
}
#endregion
#region 委托
delegate void mydelegate(int n);
class MyDeClass
{
public void fun1(int n)
{
Console.WriteLine("{0}的2倍={1}", n, 2 * n);
}
public void fun2(int n)
{
Console.WriteLine("{0}的3倍={1}", n, 3 * n);
}
}
class Program
{
static void Main(string[] args)
{
MyDeClass obj = new MyDeClass();
mydelegate p = new mydelegate(obj.fun1);
p(100);
p = new mydelegate(obj.fun2);
p(2);
}
}
#endregion
#region 委托
delegate double mydelegate(double x, double y);
class MyDeClass
{
public double add(double x, double y)
{
return x + y;
}
public double sub(double x, double y)
{
return x - y;
}
public double mul(double x, double y)
{
return x * y;
}
public double div(double x, double y)
{
return x / y;
}
}
class Program
{
static void Main(string[] args)
{
MyDeClass obj = new MyDeClass();
mydelegate p = obj.add;
Console.WriteLine("5+8={0}", p(5, 8));
p = obj.sub;
Console.WriteLine("5-8={0}", p(5, 8));
p = obj.mul;
Console.WriteLine("5*8={0}", p(5, 8));
p = obj.div;
Console.WriteLine("5/8={0}", p(5, 8));
}
}
#endregion
#region 委托
delegate void mydelegate(double x, double y);
class MyDeClass
{
public void add(double x, double y)
{
Console.WriteLine("{0}+{1}={2}", x, y, x + y);
}
public void sub(double x, double y)
{
Console.WriteLine("{0}-{1}={2}", x, y, x - y);
}
public void mul(double x, double y)
{
Console.WriteLine("{0}*{1}={2}", x, y, x * y);
}
public void div(double x, double y)
{
Console.WriteLine("{0}/{1}={2}", x, y, x / y);
}
}
class Program
{
static void Main(string[] args)
{
MyDeClass obj = new MyDeClass();
//mydelegate p, a;
//a = obj.add;
//p = a;
//a = obj.sub;
//p += a;
//a = obj.mul;
//p += a;
//a = obj.div;
//p += a;
//p(5, 8);
mydelegate p = new mydelegate(obj.add);
p += obj.div;
p += obj.mul;
p += obj.sub;
p(5, 10);
}
}
#endregion
#region 使委托与匿名方法关联
delegate void mydelegate(string mystr);//====没有方法名称的方法,当将委托与匿名方法关联时,直接给出方法的函数体====//
class Program
{
static void Main(string[] args)
{
委托类型名 对象名 返回类型 (参数列表) 匿名方法代码
mydelegate p = delegate(string mystr){Console.WriteLine(mystr);};
p("valeb");
}
}
#endregion
}
事件与委托的入门例子
最新推荐文章于 2020-06-10 16:32:26 发布