Gne*_*ock 6 c# resources overloading unity5
我正在使用C#为Unity编写代码.目前,我正在处理一些较小的类和结构,以便快速序列化随机生成的地图.在这样做时,我处理了一些构造函数,这些构造函数也将一些较小的类和结构作为参数.
在过去,我通常会在设置方法和构造函数时尝试考虑合理的选项.虽然这种做法受到了质疑,但没有人能够给我任何合理的理由,说明为什么我不应该这样做.
考虑以下类和结构:
public class Tile
{
public GridCoordinates position;
public TileCode prefabID;
}
public struct GridCoordinates
{
public int x;
public int y;
}
public struct TileCode
{
public int categoryID;
public int individuaID;
}
Run Code Online (Sandbox Code Playgroud)
通常,在创建构造函数时,我会涵盖所有struct和int替代方案.Tile看起来像这样:
public class Tile
{
public GridCoordinates position;
public TileCode prefabID;
public Tile(TileCode prefabID, GridCoordinates position)
{
this.prefabID = new TileCode(prefabID);
this.position = new GridCoordinates(position);
}
public Tile(TileCode prefabID, int xPosition, int yPosition)
{
this.prefabID = new TileCode(prefabID);
position = new GridCoordinates(xPosition, yPosition);
}
public Tile(int typeID, int individualID, GridCoordinates position)
{
prefabID = new TileCode(typeID, individualID);
this.position = new GridCoordinates(position);
}
public Tile(int typeID, int individualID, int xPosition, int yPosition)
{
prefabID = new TileCode(typeID, individualID);
position = new GridCoordinates(xPosition, yPosition);
}
Run Code Online (Sandbox Code Playgroud)
为了提高效率,我倾向于这样做.与第一个构造函数/方法一起编写额外的构造函数/方法需要花费大量的多余时间,并且当我后来希望以一种我原本没有使用的方式使用构造函数/方法时,我发现这有时会派上用场.预料.
以前提出的唯一问题是混淆的可能性.我觉得这不是一个真正的问题,因为我的组织和评论清楚地区分了每个变体.
最后,我担心我的老师和同事可能还有其他问题没有意识到.我目前正在寻求扩展到一个更大的项目,现在更容易改变我的行为,而不是以后纠正它.
如果我为课程提供过多的替代构造函数或方法,我会面临什么问题?