我有一个类,大致设计如下:
class Vector3
{
float X;
float Y;
float Z;
public Vector3(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
}
我有其他类将其实现为属性,例如:
class Entity
{
Vector3 Position { get; set; }
}
现在我使用以下代码设置实体的位置:
myEntity.Position = new Vector3(6, 0, 9);
我想通过为 Vector3 实现类似数组的初始化器来简化用户的操作:
myEntity.Position = { 6, 0, 9 };
然而,没有任何类可以继承数组。此外,我知道我可以通过一些小技巧来解决这个问题:
myEntity.Position = new[] { 6, 0, 9 };
但这不是重点。:)
谢谢!