简体   繁体   English

C#继承层次结构中的对象创建

[英]Object creation in C# inheritance hierarchy

I have below two classes. 我有两节课。

在此输入图像描述

When execute this line, 执行此行时,

Child myChildObj = new Child();

does this create separate two objects ( Parent and Child ) ? 这会创建单独的两个对象ParentChild吗? Or just a single child object which includes both the parent methods and attributes ? 或者只是一个包含父方法和属性的子对象?

在此输入图像描述

Update: I want to know in CLR, whether it actually creates a Parent object (which is not accessible) at the runtime. 更新:我想知道在CLR中,它是否实际上在运行时创建了一个Parent对象(无法访问)。

I have seen the following quote in tutorialspoint website. 我在tutorialspoint网站上看​​到了以下引用。

The derived class inherits the base class member variables and member methods. 派生类继承基类成员变量和成员方法。 Therefore the super class object should be created before the subclass is created. 因此,应在创建子类之前创建超类对象。 You can give instructions for superclass initialization in the member initialization list. 您可以在成员初始化列表中提供超类初始化的说明。

I have used the following code to verify this, and I got the same hashCode value for both child and parent objects. 我使用以下代码来验证这一点,并且我为子对象和父对象获得了相同的hashCode值

Console.WriteLine("child object hashcode : "+this.GetHashCode());
Console.WriteLine("base object hashcode : "+base.GetHashCode());

This will create a single instance of the Child type which includes methods from both types. 这将创建一个Child类型的单个实例,其中包含两种类型的方法。 You could call those methods on the created instance. 您可以在创建的实例上调用这些方法。

Child is a subclass of Parent , so it inherits all its properties. ChildParent的子类,因此它继承了它的所有属性。 But when you create an object of Child , you only get an object of Child . 但是当您创建Child的对象时,您只能获得Child的对象。 There is no “second part” that takes care of the Parent -related things. 没有“第二部分”来处理与Parent相关的事情。 There only exists a single object in the memory. 内存中只存在一个对象。 And that single object has the type Child and as such is also compatible to the Parent type. 并且该单个对象具有Child类型,因此也与Parent类型兼容。

The answers given are all as abstract as what you learned from the books and I feel you want to know what it looks like under the covers. 给出的答案都与你从书本中学到的一样抽象,我觉得你想知道它在幕后的样子。 You can think of the child instance as a piece of memory that is exactly what you would get if you would instantiate a parent object, only with some extra bits and pieces appended to it (the child bits). 您可以将子实例视为一块内存,如果您实例化一个父对象,只需要添加一些额外的位和部分(子位),就可以得到它。 The reference you get is a pointer to the beginning, that is the base class instance. 您获得的引用是指向开头的指针,即基类实例。 This is why no one will ever see the difference between a parent instance and a child instance. 这就是为什么没有人会看到父实例和子实例之间的区别。 Because the child instance is a parent instance (with the child bits appended to it). 因为子实例是父实例(附加了子位)。 This goes on as you instantiate GrandChild objects, those would also be the same with yet some extra bits appended to it. 这会在您实例化GrandChild对象时继续,这些对象也会添加一些额外的位。 Now you understand why and when casting works and when it would cause trouble. 现在您了解为什么以及何时进行投射以及何时会造成麻烦。

It creates just one instance of class "Child". 它只创建一个类“Child”的实例。 The instances of "Child" inherits (includes) all the members from "Parent" class. “Child”的实例继承(包括)“Parent”类中的所有成员。

There is only one referenceable object created which is myChildObj . 只创建了一个可引用的对象myChildObj The parent class is used as a template but is not ultimately instantiated. 父类用作模板,但最终未实例化。 This object will have all member variables and methods of the Parent class as well as the variables and methods in its own class. 该对象将包含Parent类的所有成员变量和方法以及其自己的类中的变量和方法。

It is also possible to create the object using polymorphism like this: 也可以使用多态来创建对象,如下所示:

Parent obj = new Child();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM