怎么可以在基类的一个实例持有派生类的一个实例?

怎么可以在基类的一个实例持有派生类的一个实例?

问题描述:

我一直是一个净codeR(不能说我是一个程序员)2年。有一个问题,我无法理解多年,这是怎么可能的基类的一个实例持有派生类的一个实例?

I have been a .Net coder (can not say I am a programmer) for 2 years. There is one question that I can not understand for years, that is how could an instance of the base class hold an instance of the derived class?

假设我们有两大类:

class BaseClass
{
    public A propertyA;
    public B propertyB;
}

class DerivedClass :BaseClass
{
    public C propertyC;

}

这怎么可能发生:

How could this happen:

BaseClass obj = new DerivedClass ()

我的意思是,的BaseClass 的内存模型,先后为新增propertyC没有空间,所以怎么可能仍持有propertyC的价值呢?

I mean, the memory model of BaseClass, has no space for the newly added propertyC, so how could it still hold the value of propertyC?

在另一边,怎么会这样是不可能发生的:

On the other side, how could this can not happen:

DerivedClass  obj = new BaseClass()

我想这是因为 DerivedClass 的内存模型的正确方法已为BaseClass的,甚至更多的所有空间。但是,这是不正确的,为什么呢?

I thought this is the correct way since the memory model of DerivedClass has all the spaces for the BaseClass and even more. But this is not true, why?

我知道,我问一个非常愚蠢的问题,但也有人给我这样一个更详细的答案?这将是从存储器或编译器的透视更好

I know I am asking a really stupid question, but could someone give me a more detail answer of this? It would be better from the perspective of the memory or compiler.

由于(此概念经常被误解)持有的指针变量是从实际的具体类型的对象,它指向independantly类型。

Because (and this concept is often misunderstood) the variable that holds the pointer is typed independantly from the actual concrete type of the object it points to.

所以,当你写

BaseClass obj = new DerivedClass () 

部分,上面写着 BaseClass的OBJ 创建上,编译器理解举行一个参考的东西,是堆栈一个新的参考变量或导出,从的BaseClass

the part that says BaseClass obj creates a new reference variable on the Stack that the compiler understands to hold a reference to something that is, or derives, from BaseClass.

这读取部分 =新DerivedClass()实际上创建了一个新的对象类型 DerivedClass ,将其存储在堆,并存储的指针的在内存中的位置是对象 OBJ 点。在堆的实际的对象是 DerivedClass ;这被称为的具体类型的对象。

The part that reads = new DerivedClass () actually creates a new object of type DerivedClass, stores it on the Heap, and stores a pointer to that object in the memory location that obj points to. The actual object on the Heap is a DerivedClass; this is referred to as the concrete type of the object.

变量 OBJ 被声明为类型的BaseClass 。这不是一个的具体类型的,它仅仅是一个的变量类型的,它只能从存储的地址到变量限制编译器的指向的一个对象,它是不是一个的BaseClass 或不从类型派生的BaseClass

The variable obj is declared to be of type BaseClass. This is not a concrete type, it is just a variable type, which only restricts the compiler from storing an address into the variable that points to an object which is not a BaseClass or which does not derive from the type BaseClass.

这是为了保证无论你投入obj变量,无论它是一个具体类型的BaseClass DerivedClass 对code>,这将有键入的BaseClass 的所有方法,属性和其他成员。它告诉编译器的 OBJ 变量的所有使用应仅限于类的唯一成员的BaseClass 用途

This is done to guarantee that whatever you put into the obj variable, no matter whether it is a concrete type of BaseClass or DerivedClass, it will have all the methods, properties and other members of the type BaseClass. It tells the compiler that all uses of the obj variable should be restricted to uses of only those members of the class BaseClass.