对象创建在内存中的引用

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

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;
        }
    }

对应代码 自理解图

 

posted @ 2020-09-12 23:01  zklve2  阅读(165)  评论(0编辑  收藏  举报