代码背景:
定义全局变量 private object currentObj=new object();
for(i=0;i<objectList.count;i++) { currentObj=objectList[i]; Queue.Enqueue(currentObj) }
costomer()==>>object obj=Queue.Dequeue();
这时候发现obj都变成了最后一个对象;
分析:其实每个对象是引用地址,当每次进行currentObj=objectList[i];时,都将这个currentObj地址中的内容改变,而在队列中的每个成员都是currentObj所指向的地址,所以都是一样的。
解决方法:
1)object对象的构造函数进行重写,重写一个带参的构造函数(如下),然后currentObj=objectList[i];改为currentObj=new object(objectList[i]);
object(object obj) { this.x=obj.x; this.y=obj.y; }
2) 中间临时变量接一下,再进行入队列,其实原理是一样的就是将一个新地址加入队列,这样就不会再一样了!