本文介绍了为什么实例会改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嘿伙计 我有一个对象的实例说: List< Object> myList = new List< Object>(); Object myObject = new Object(); myObject.PositionVector = new Vector3(10,10,10 ); myList.Add(myObject); 使用上面的代码作为一个准确的例子,我在循环中这样做。在每个 迭代中我改变位置向量,但是当我这样做并检查我的 列表时,它已经正确地将每个新实例添加为单独的条目,但是它们是 都有最后一个要添加的位置。当我在调试中通过它来验证时,这已被验证,正确的位置详细信息在一个 迭代中,并且在新实例具有 它的位置也改变了我的列表中的位置。 我认为没有进行新的内存分配等设置 位置它会覆盖我的列表正在查看的相同内存,但为什么呢?我认为''新''运算符明确告诉它创建一个新内存 分配?我错过了什么吗?任何人都知道如何防止这种情况。 谢谢Hey guysI have an instance of an object say:List<Object> myList = new List<Object>();Object myObject = new Object();myObject.PositionVector = new Vector3(10,10,10);myList.Add(myObject);With the above code as an accurate example i do that in a loop. And on eachiteration i change the position vector, however when i do this and check mylist it has correctly added each new instance as a separate entry but theyall have the position of the last one to be added. When i steped through itin debug this was verified, the correct positon details are in on oneiteration and on the next iteration immediately after the new instance hasits position changed so too does the one already in my List.I presume that a new memory allocation isn''t being made and so on settingposition it overwrites the same memory my list is looking at but why? Ithought the ''new'' operator explicitly tells it to create a new memoryallocation? Am i missing something? Anyone know how i can prevent thissituation.Thanks推荐答案 这篇关于为什么实例会改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!