using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace delegateDemo
{
class Program
{
static void OtherClassMethod()
{
Console.WriteLine("another class's static method");
} static void Main(string[] args)
{
var test = new TestDelegate();
test.delegateMethod = new TestDelegate.DelegateMethod(test.NonStaticMethod);
test.delegateMethod += new TestDelegate.DelegateMethod(TestDelegate.StaticMethod);
test.delegateMethod += Program.OtherClassMethod;
test.RunDelegateMethods();
}
} class TestDelegate
{
public delegate void DelegateMethod(); //声明了一个Delegate Type public DelegateMethod delegateMethod; //声明了一个Delegate对象
public static void StaticMethod()
{
Console.WriteLine("Delegate a static method");
} public void NonStaticMethod()
{
Console.WriteLine("Delegate a non-static method");
} public void RunDelegateMethods()
{
if (delegateMethod != null)
{
Console.WriteLine("---------");
delegateMethod.Invoke();
Console.WriteLine("---------");
}
}
} }
一个简单的Delegate使用例子
转载
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
一个简单的RESTful例子
首先感谢本文作者,群里的朋友:金刀不无敌 以下是作者整理的干货……
json 字段 逻辑运算 -
synchronized的一个简单例子
ain(String[] args) { // TODO 自动生成方法存根 TxtThread tt = new TxtT...
同步方法 示例代码 多线程 get方法 java