llllluuuca 2021-11-21 00:58 采纳率: 100%
浏览 14
已结题

c# 一个有类参数的类会和类参数同增同减是为什么

为什么不带注释的那组代码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; }
//}

}

  • 写回答

2条回答 默认 最新

查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月29日
  • 已采纳回答 11月21日
  • 创建了问题 11月21日

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了