C#中的内存分配 [英] Memory allocation in C#

查看:341
本文介绍了C#中的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在继承中,我们创建了父类和子类的对象。我们用子类对象分配子类的内存和子类对象的子类的内存。父类的第1次运行时对象调用子类的构造函数类然后是父类。

我的问题是,,,是否存在父类对象和子类对象的内存分配都可能,,,&将使用其内存并​​且将浪费... ..which类可以使用另一个类的内存吗??

In inheritance we make an object of parent class & child class.we allocate the memory of child class with parent class object & memory of child class with child class object.At run time object of parent class 1st call the constructor of child class then parent class.
My question is,,, is there memory allocation of both parent class object & child class object both is possible,,,& whose memory will used & whose will be waste....which class can use the memory of another class ??

推荐答案

你在说什么?你的继承概念是有缺陷的:

在父类1的运行时对象中调用子类的构造函数然后调用父类。否。错误的方法。

如果从类继承,则首先调用基类构造函数:如果不是,则派生类不能依赖或使用任何基类字段,构造函数中的属性或方法,这将使它几乎无用。



没有浪费内存(除非派生类不使用基类功能。如果你创建Base类的一个对象,分配的内存将是基类对象的大小。如果你创建一个Derived类的实例,分配将是派生类的大小。

At基础类实例的创建没有时间根据任何派生类的大小分配内存 - 这是您无法将基类实例强制转换为派生类实例的原因之一。
What are you talking about? Your concept of inheritance is flawed:
"At run time object of parent class 1st call the constructor of child class then parent class".No. Wrong way round.
If you inherit from a class, then the base class constructor is called first: if it wasn't then the derived class could not rely on or use any of the base class fields, properties or methods in the constructor, which would make it pretty much useless.

No memory is wasted (unless the derived class does not use base class features. If you create an object of the Base class, the memory allocated will be the size of a base class object. If you create an instance of a Derived class, the allocation will be the size of the derived class.
At no time will the creation of a base class instance allocate memory based on the size of any derived class - that is one of the reasons why you cannot cast a base class instance to a derived class instance.


这篇关于C#中的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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