C# - 在继承中如何CLR组织内存/参考? [英] C# - How does CLR organize memory/reference during the inheritance?

查看:113
本文介绍了C# - 在继承中如何CLR组织内存/参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有code片断如下:(澄清目的/不规范

 类员工
{
    #地区的字段

    保护字符串_empID;

    保护字符串_empName;

    保护只读字符串_ssn;

    #endregion

    公务员(){}

    公务员(字符串_empID,串_empName,串_ssn)
    {
        this._empID = _empID;
        this._empName = _empName;
        this._ssn = _ssn;
    }
}


一流的管理:员工
{
    私人字符串_branchID;

    公共管理(INT _branchID):基地(字符串_empID,串_empName,串_ssn)
    {
        this._branchID = _branchID;
    }
}

静态无效的主要()
{
   经理经理=新的管理器(1,山姆,XXX,分支1);
}
 

使用基础 关键字,我调用父类的构造

在这种情况下,如何继承的组织?我有一些坏的假设如下:

  

由于管理从员工获得,管理类充满了(EMPID,empName,SSN)

  -----------------
 经理
-----------------
EMPID
empName
SSN
branchID
 

步骤1:构造调用:碱(1,SAM,XXX)

步骤2:基类(员工)构造充满派生类申请(EMPID,empName,SSN)

第3步:branchID由派生类的构造函数分配

......

我的问题是

    

  • 如果一个类从基类派生,派生类也有隐藏在基类的领域?
  • 派生类股基类领域?我的意思是单独的内存块分配给基类和派生类?
  • 解决方案

    是的,在派生类中也将包含在内存中的基类的字段。在通过C#CLR 112页,杰弗里里希特说:

    那么,M3执行其code构建一个管理对象。这会导致管理类型,管理对象,将在托管堆中创建,如图4-9的实例。正如你可以看到,管理对象,因为这样做的所有对象,有一个类型的对象指针和同步块索引。此对象还包含必要的字节数以容纳所有的管理类型定义的实例数据字段定义的任何实例字段由管理型(,员工和对象在这种情况下)的任何基类。 (强调)

    另外值得一提的是独立的存储块的的任何基本类型创建。但只适用于数据类型(所以只有一次,最多)。当你创建一个管理对象(源于员工),CLR将确保有一个管理器类型对象,和职业类型的对象在堆上。

    1)发生里氏,杰弗里。通过C#CLR。微软:微软preSS,2006年。 (亚马逊链接

    Suppose i have the code snippet as follows : ( clarification purpose/not well formed)

    class Employee
    {
        #region fields
    
        protected string _empID;
    
        protected string _empName;
    
        protected readonly string _ssn;
    
        #endregion
    
        public Employee(){}   
    
        public Employee(string _empID,string _empName,string _ssn)
        {
            this._empID =_empID;
            this._empName=_empName;
            this._ssn = _ssn;
        }
    }
    
    
    class Manager : Employee
    {
        private string _branchID;
    
        public Manager(int  _branchID):base(string _empID,string _empName,string _ssn)
        {
            this._branchID=_branchID;
        }
    }
    
    static void Main()
    {
       Manager mgr = new Manager("1","sam","xxx","Branch1");
    }
    

    Using base keyword I am invoking parent class constructor .

    In this case how the inheritance is organized? I have some bad assumptions as follow:

    As Manager is derived from Employee, Manager class is filled with (empID,empName,ssn)

    -----------------
     Manager
    -----------------
    empID
    empName
    ssn
    branchID
    

    step 1 : constructor invoking :base( "1","sam","xxx")

    step 2 : base class(Employee) constructor fills the derived class filed (empID,empName,ssn)

    step 3 : branchID is assigned by derived class constructor

    .......

    My question is

  • If a class is derived from base class, the derived class also has the base class' hidden fields?
  • Derived class shares Base class fields? I mean separate memory block is allocated for Base class and derived class?
  • 解决方案

    Yes, the derived class will also contain the base class fields in memory. On page 112 of CLR via C#, Jeffrey Richter says:

    "Then, M3 executes its code to construct a Manager object. This causes an instance of the Manager type, a Manager object, to be created in the managed heap, as shown in Figure 4-9. As you can see, the Manager object-as do all objects-has a type object pointer and sync block index. This object also contains the bytes necessary to hold all of the instance data fields defined by the Manager type as any instance fields defined by any base classes of the Manager type (in this case, Employee and Object)." (Emphasis Added)

    It's also worth noting that a separate block of memory is created for any base types. But ONLY for the type data (so only once, at most). When you create a Manager object (which derives from Employee), the CLR will ensure that there is a Manager Type Object, and an Employee Type Object on the heap.

    1) Richter, Jeffrey. CLR via C#. Redmond: Microsoft Press, 2006. (Amazon Link)

    这篇关于C# - 在继承中如何CLR组织内存/参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆