分配多少内存? [英] How much memory will allocated?

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

问题描述

class A
{
    public A()
    {
        Console.WriteLine("Default A");
    }

    public A(int a)
    {
        Console.WriteLine("Parameterized A");
    }
}

class B : A
{
    public B()
    {
        Console.WriteLine("Default B");
    }

    public B(int a)
    {
        Console.WriteLine("Parameterized B");
    }
}





我尝试了什么:





What I have tried:

How much memory will allocate if we call:

A a = new B();

and

A a = new A();

推荐答案

两者都相同:内存分配是类包含的字段(加上类类型标识符),而不是类包含的实际方法。其中一个副本就是存在的,这些都存在于代码中,而不是堆或堆栈上。
The same for both: the memory allocation is the fields the class contains (plus a class type identifier) not the actual methods the class contains. One copy of those is all that exists, and that's in your code, not on the heap or stack.


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

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