c#类初始化 [英] c# class initialization

查看:387
本文介绍了c#类初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个类被另一个类初始化时会发生什么:

What happens when a class is initialized by another class like :

class a
{
   
}
class b
{

}
class c
{
   static void Main(String aa[])
   {
    a obj=new b();
   }

}



谢谢你!!!


thank u!!!

推荐答案

一旦你创建了一个类的实例,它就会被保存在内存中,直到父类执行结束。

这个实例可以用于垃圾收集。
Once you create an instance of a class, it is kept in memory till the parent class execution ends.
This instance is then viable for garbage collection.


试想一下:创建对象的区别是什么?方法是方法,无论什么类。想想,只需使用一些逻辑。







你需要了解两个内存对象涉及:引用本身和引用的对象。如果对象变得无法访问,它最终将被垃圾收集:



http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 [ ^ ]



在你的情况下,你不会成为能够使用您创建的项目:您创建它并在返回方法时忘记引用。所以,它没有任何实际意义。



-SA
Just think about it: what''s the difference where the object is created? Method is method, no matter of what class. Think, just use some logic.



You need to understand that two memory objects are involved: a reference itself and a referenced object. If the object becomes unreachable, it will be eventually garbage-collected:

http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^]

In your case, you won''t be able to use the project you have created: you create it and forget the reference when the method is returned. So, it makes no practical sense.

—SA


你不能创建一个对象b除非您将A声明为父类,将B声明为子类,否则参考A。下面的行将给你编译时错误。



You cannot create an object of b with reference of A unless You declare A as parent class and B as child class. The below line will give you compile time error.

a obj=new b();







If you want to derive B class as reference of A,then declare
class B as child class
like this
class a
{

}
class b:A
{

}


这篇关于c#类初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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