同一个类的不同实例的实现

方法一:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Class1
    {
        static void Main(System.String[] args)
        {
            for (System.Int32 i = 0; i < 5;i++ )
            {
                new Set(3).ProcessingItem(Class1.FeedbackToConsole);
            }
            Console.Read();
        }
        static void FeedbackToConsole(System.Object value,System.Int32 item,System.Int32 length,System.Int32 id)
        {
            Console.WriteLine("Processing item[{1}] of {2}:{0}-----{3}",value,item,length,id);
        }
    }

    class Set
    {
        private System.Object[] items;
        private System.Int32 _id;
        public System.Int32 Id
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }
        public Set(System.Int32 numItem)
        {
            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;
    }

}

方法二:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyObject myObject = new MyObject(5);
            MyObject.Feedback fb = new MyObject.Feedback(Program.FeedbackToConsole);
            //MyObject.Feedback fb2 = new MyObject.Feedback(Program.FeedbackToConsole);
            for (System.Int32 i = 0; i < 5;i++ )
            {
                fb += new MyObject.Feedback(Program.FeedbackToConsole);
                new MyObject(i + 1).ProcessingItem(fb);
            }
           
            //myObject.ProcessingItem(fb);
            //System.Delegate result=System.Delegate.Combine(fb,fb2);
 
            //myObject.ProcessingItem(result as MyObject.Feedback);
            //myObject.ProcessingItem(fb);
            //Console.WriteLine("fb.Equals(fb2):{0}",fb.Equals(fb2));
            Console.Read();
        }

        private static void FeedbackToConsole(System.Object value,System.Int32 item,System.Int32 numItems,System.String str)
        {
            Console.WriteLine("Processing items[{1}] of {2}:{0}-----{3}",value,item,numItems,str);
        }
    }

    class MyObject
    {
        private System.Object[] items;
        private System.String _id;

        public System.String Id
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }

        public MyObject(System.Int32 numItems)
        {
            items=new System.Object[numItems];
            for (System.Int32 i = 0; i < numItems;i++ )
            {
                items[i] = i;
            }
            this._id = ProcessingID();
        }

        public System.String ProcessingID()
        {
            //System.Threading.Thread.Sleep(3000);
            Console.WriteLine("-------------这是一个实例--------------");
            return System.String.Format("Object[{0}]", System.Guid.NewGuid());
        }

        public delegate void Feedback(System.Object value,System.Int32 item,System.Int32 numItems,System.String index);

        public void ProcessingItem(Feedback feedback)
        {
            if(feedback!=null)
            {
                for (System.Int32 i = 0; i < items.Length;i++ )
                {
                    feedback(items[i], i + 1, items.Length, System.String.Format("The index of Object:{0}", this.Id));
                    //feedback.Invoke(items[i], i + 1, items.Length);
                }
            }
        }
    }

}

总结:就是添加一个标志属性,具体实现可以是System.Guid.NewGuid()(方便)和在实体类外定义一个static变量

posted @ 2012-04-06 10:51  szjdw  阅读(280)  评论(0编辑  收藏  举报