利用委托链实现非静态变量初始化一次,但是构造函数执行多次
2012-04-06 11:26
363 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Class1
{
static void Main(System.String[] args)
{
Set.Feedback fb= new Set.Feedback(Class1.FeedbackToConsole);
for (System.Int32 i = 0; i < 5;i++ )
{
new Set(3).ProcessingItem(fb);
fb += new Set.Feedback(Class1.FeedbackToConsole);
}
Console.Read();
}
static void FeedbackToConsole(System.Object value,System.Int32 item,System.Int32 length,System.Int32 id)
{
Console.WriteLine("{4},Processing item[{1}] of {2}:{0}-----Set[{3}]",value,item,length,id,Set.times++);
}
}
class Set
{
private System.Object[] items;
private System.Int32 _id=0;
public static System.Int32 times=1;
public System.Int32 Id
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
public Set(System.Int32 numItem)
{
times = 1;
this._id = Identify.id++;
Console.WriteLine("产生新的实例{0}",this._id);
items = new System.Object[numItem];
for (System.Int32 i = 0; i < numItem;i++ )
{
items[i] = i;
}
}
public delegate void Feedback(System.Object value,System.Int32 item,System.Int32 itemLenth,System.Int32 id);
public void ProcessingItem(Feedback feedback)
{
if(feedback!=null)
{
for (System.Int32 i = 0; i < items.Length;i++ )
{
feedback(items[i],i+1,items.Length,this._id);
}
}
}
}
class Identify
{
public static System.Int32 id = 0;
}
}
执行结果:
总结:
1,利用属性和外加静态int32可以区分同一个类的不同实例;
2,利用静态字段和构造函数初始化可以实现一个实例的一个方法执行次数的统计;
3,利用委托可以实现具体方法实现的分离,实现约束方法签名,方法实现多态化。
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Class1
{
static void Main(System.String[] args)
{
Set.Feedback fb= new Set.Feedback(Class1.FeedbackToConsole);
for (System.Int32 i = 0; i < 5;i++ )
{
new Set(3).ProcessingItem(fb);
fb += new Set.Feedback(Class1.FeedbackToConsole);
}
Console.Read();
}
static void FeedbackToConsole(System.Object value,System.Int32 item,System.Int32 length,System.Int32 id)
{
Console.WriteLine("{4},Processing item[{1}] of {2}:{0}-----Set[{3}]",value,item,length,id,Set.times++);
}
}
class Set
{
private System.Object[] items;
private System.Int32 _id=0;
public static System.Int32 times=1;
public System.Int32 Id
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
public Set(System.Int32 numItem)
{
times = 1;
this._id = Identify.id++;
Console.WriteLine("产生新的实例{0}",this._id);
items = new System.Object[numItem];
for (System.Int32 i = 0; i < numItem;i++ )
{
items[i] = i;
}
}
public delegate void Feedback(System.Object value,System.Int32 item,System.Int32 itemLenth,System.Int32 id);
public void ProcessingItem(Feedback feedback)
{
if(feedback!=null)
{
for (System.Int32 i = 0; i < items.Length;i++ )
{
feedback(items[i],i+1,items.Length,this._id);
}
}
}
}
class Identify
{
public static System.Int32 id = 0;
}
}
执行结果:
总结:
1,利用属性和外加静态int32可以区分同一个类的不同实例;
2,利用静态字段和构造函数初始化可以实现一个实例的一个方法执行次数的统计;
3,利用委托可以实现具体方法实现的分离,实现约束方法签名,方法实现多态化。
相关文章推荐
- C#利用子窗体中的构造函数 (实现了父窗体给子窗体的传值,但是子窗体的值回传暂未实现)
- 利用UDPSocket模拟一次对话,只能运行依次,多次执行还在测试中.跪求大神建议....
- 利用 构造函数 实现数据成员的 初始化
- Linux Bash代码 利用for循环实现命令的多次执行
- MFC 线程创建在使用构造函数进行初始化线程的时候,创建线程实例了,但是线程并没有执行的问题
- python 模块被引用多次但是里面的全局表达式总共只会执行一次
- 利用 stringstream 和 getline 实现仅通过一次键盘录入初始化整个数组
- js点击事件绑定一次,但是点击却执行了多次
- 利用C++的operator new实现同一对象多次调用构造函数
- 利用C++的operator new实现同一对象多次调用构造函数
- 利用委托来实现关闭子窗口通知父窗口执行方法
- 利用脚本实现mysql主库到备库数据同步(每五分钟同步一次增量)
- 分享:利用类继承实现stl map 初始化
- 利用Java反射实现JavaBean对象相同属性复制并初始化目标对象为空的属性的BeanUtils
- shell脚本实现每秒执行一次任务
- 在.NET中利用委托实现窗体间安全通信
- java变量,初始化快,构造函数的执行顺序
- .NET如何利用timer组件实现每天定时执行某项任务 .
- C++11实现模板手柄:委托构造函数、defaultkeyword分析
- C++利用time_t来实现一个周期性执行程序