为什么不带注释的那组代码controller1.boy.Age和yboy.Age绑定在了一起,只要一个增加或减少另外一个也会增加或减少
但是带注释的那组代码就不会发生改变
class Program
{
static void Main(string[] args)
{
Boy yboy = new Boy() { Age = 20 };
Controller controller1 = new Controller(yboy);
yboy.Age = 100;
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
controller1.boy.Age = 200;
Boy xboy = new Boy() { Age = 10 };
Controller controller2 = new Controller(xboy);
Console.WriteLine(controller1.boy.Age);
Console.WriteLine("-----------------------");
Console.WriteLine(controller2.boy.Age);
Console.WriteLine("---------------------------------");
Console.WriteLine(xboy.Age);
Console.WriteLine(yboy.Age);
Console.ReadKey();
//Girl xgirl = new Girl() { Age = 10 };
//Calculator calculator = new Calculator(xgirl);
//xgirl.Age += 100;
//Console.WriteLine(xgirl.Age);
//Console.WriteLine(calculator.xxx);
//Console.ReadKey();
}
}
class Controller
{
public Boy boy;
public Controller(Boy _boy)
{
if (_boy.Age <= 15)
{
this.boy = _boy;
this.boy.Age += 1;
}
else
{
this.boy = _boy;
}
}
}
class Boy
{
public int Age { get; set; }
}
//class Calculator
//{
// public int xxx { get; set; }
// public Calculator(Girl girl)
// {
// if (girl != null)
// {
// this.xxx = girl.Age;
// this.xxx -= 1;
// }
// }
//}
//class Girl
//{
// public int Age { get; set; }
//}
}