考虑这个类
class SomeObject
{
private int x;
public int X
{
get { return x; }
set { x = value+z; }
}
private int y;
public int Y
{
get { return y; }
set { y = value+z ; }
}
private int z;
public int Z
{
get {return z;}
set { z = value+y ; }
}
}
当我做类似的事情时
var r = new SomeObject() {X = 1, Y = 1, Z = 1 };
Console.WriteLine(r.X + " " + r.Y + " " + r.Z);
var r1 = new SomeObject() { Y = 1, X = 1, Z = 1 };
Console.WriteLine(r1.X + " " + r1.Y + " " + r1.Z);
var r2 = new SomeObject() { Z = 1, X = 1, Y = 1 };
Console.WriteLine(r2.X + " " + r2.Y + " " + r2.Z);
控制台输出
1 1 2
1 1 2
2 2 1
除了使用构造函数之外,还有其他方法可以解决此问题吗?
正确的输出应为2 2 2
每个答案都很有帮助,但我不能将它们都标记为最佳答案