改变一个对象的属性,改变所有对象,为什么?
Changing Properties in one object, changes in all, why?
我正在编写一个简单的地图创建器,当我注意到通用地图中的某些图块正在更改,而不是稍后将合并到通用地图中的本地地图中...所以我开始测试一些东西,并且由于某种原因,这种情况发生了。如果我更改类型 属性 为什么它 return 相同?
对第一个或第二个拼贴的任何 属性 所做的任何更改都适用于两者。
Tile first = new Tile(new CoordInt(0, 0), tileType.Wall);//create first tile new Tile(new CoordInt(int,int), (Enum)tileType)
Tile second = first;//NEW Tile second equals first
second.SetType(tileType.Floor);//property Type of the Tile named second is set to Floor (property set to { get; private set; })
Debug.Log(first.ToLongString());
Debug.Log(second.ToLongString());
Debug.Log(first == second);
//Console
//Tile at (0, 0) from room -1 is a Floor and is Neutral
//You CANNOT walk in it
//Tile at (0, 0) from room -1 is a Floor and is Neutral
//You CANNOT walk in it
//True
//Why does this return the same, if i'm changing the property only of the Tile named second and not of both?
您需要复制该对象。 First 和 second 引用内存中的同一个对象。您可以向 class 添加复制方法。
我正在编写一个简单的地图创建器,当我注意到通用地图中的某些图块正在更改,而不是稍后将合并到通用地图中的本地地图中...所以我开始测试一些东西,并且由于某种原因,这种情况发生了。如果我更改类型 属性 为什么它 return 相同?
对第一个或第二个拼贴的任何 属性 所做的任何更改都适用于两者。
Tile first = new Tile(new CoordInt(0, 0), tileType.Wall);//create first tile new Tile(new CoordInt(int,int), (Enum)tileType)
Tile second = first;//NEW Tile second equals first
second.SetType(tileType.Floor);//property Type of the Tile named second is set to Floor (property set to { get; private set; })
Debug.Log(first.ToLongString());
Debug.Log(second.ToLongString());
Debug.Log(first == second);
//Console
//Tile at (0, 0) from room -1 is a Floor and is Neutral
//You CANNOT walk in it
//Tile at (0, 0) from room -1 is a Floor and is Neutral
//You CANNOT walk in it
//True
//Why does this return the same, if i'm changing the property only of the Tile named second and not of both?
您需要复制该对象。 First 和 second 引用内存中的同一个对象。您可以向 class 添加复制方法。