public class Test
{
//定义委托
public delegate void D_Math(int a, int b);
public void Add(int a, int b)
{
Console.WriteLine("Add方法结果:{0}", a + b);
}
public void Cut(int a, int b)
{
Console.WriteLine("Cut方法结果:{0}", a - b);
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Test t = new Test();
Test.D_Math D = new Test.D_Math(t.Add);
//委托实例化,也可Test.D_Math D =t.Add;
D += t.Cut;
//委托可以以队列方式执行多个方法,
//以+=运算符或者-=来增加或者取消队列中的方法
D(5, 6);
}
}
public class A { }
public class B:A { }//B继承自A
public class Test
{
//定义委托
public delegate A D_Math();
public B Add()
{
return new B();
}
public A Add2()
{
return new A();
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Test.D_Math d = new Test.D_Math(new Test().Add);
//委托返回A,而Add方法返回B,此为协变。
}
}
public class A { }
public class B:A { }//B继承自A
public class Test
{
//定义委托
public delegate void D_Math(B b);
public void Add(B b)
{
}
public void Add2(A a)
{
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Test.D_Math d = new Test.D_Math(new Test().Add2);
//委托引入参数B,而Add方法参数为A类型,此为协逆变。
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有