• 对象创建在内存中的引用


    创建一个对象,他在堆内存中还是在栈内存中

    Person p =new Person(“张三”,20);
    这样的,我可不可以这样理解new Person(“张三”,20)在堆内存中创建,分配内存地址。
    Person p 在栈内存中创建,然后把堆内存中的内存地址付给栈内存中的p变量

                Common common = new Common { Name = "张三" };
    
                Service service = new Service(common);
    
                //都是 指向李四
                common.Name = "李四";
                //service.dao = new Dao(common);
                //还是 李四 unbelieveable
                common = null;
    
                common = new Common { Name = "王五" };
                Console.ReadLine();
    
      public class Common
        {
            public string Name { get; set; }
        }
    
        public class Service
        {
            public Common _com;
            public Dao dao;
            public Service(Common comm )
            {
                this._com = comm;
                this.dao = new Dao(comm);
            }
        }
    
        public class Dao
        {
            public Common _com;
            public Dao(Common comm)
            {
                this._com = comm;
            }
        }

    对应代码 自理解图

  • 相关阅读:
    HDNOIP201404最短路径
    BJOI2015 Day3
    BJOI2015 Day2
    BJOI2015 Day1
    BZOJ4012 [HNOI2015]开店
    hdu2159(二维完全背包)
    hdu3496(二维背包)
    hdu3033(变形分组背包)
    hdu1267(递推)
    hdu1503(最长公共子序列)
  • 原文地址:https://www.cnblogs.com/kunlunmountain/p/13659231.html
Copyright © 2020-2023  润新知