我对类和对象有一些小疑问.
在课堂上我有10到20种方法,(下面分享)
public class tstCls
{
public void a1()
{ }
void a2()
{ }
void a3()
{ }
void a4()
{ }
void a5()
{ }
void a6()
{ }
void a7()
{ }
void a8()
{ }
void a9()
{ }
}
Run Code Online (Sandbox Code Playgroud)
为上述类创建对象时.内存中会存储多少种方法?只有调用方法或所有方法.
static void Main(string[] args)
{
tstCls objcls = new tstCls();
objcls.a1();
}
Run Code Online (Sandbox Code Playgroud)
你可以帮我解决上面的情况.
没有.
实例化对象时,不会在内存中创建方法,只有字段和属性.
只要引用程序集的任何部分,就会加载包含这些方法的程序集.
方法只有在被调用时才会被编译到内存中.JIT(Just-In-Time编译器)在那个时刻将IL的方法转换为机器代码.