官术网_书友最值得收藏!

Understanding constructors and destructors

When you ask the programming language to create an instance of a specific class, something happens under the hood. The programming language runtime creates a new instance of the specified type, allocates the necessary memory, and then executes the code specified in the constructor. When the runtime executes the code within the constructor, there is already a live instance of the class. Thus, you have access to the attributes and methods defined in the class. However, as you might have guessed, you must be careful with the code you put within the constructor, because you might end up generating large delays when you create instances of the class.

Tip

Constructors are extremely useful to execute setup code and properly initialize a new instance.

So, for example, before you can call the CalculateArea method, you want the Width and Height attributes for each new Rectangle instance to have a value initialized to 0. Constructors are extremely useful when we want to define the attributes of the instances of a class right after their creation.

Sometimes, we need specific arguments to be available when we are creating an instance. We can design different constructors with the necessary arguments and use them to create instances of a class. This way, we can make sure that there is no way of creating specific classes without using the authorized constructors that ask for the necessary arguments.

At some point, your application won't need to work with an instance anymore. For example, once you calculate the perimeter of an ellipse and display the results to the user, you don't need the specific Ellipse instance anymore. Some programming languages require you to be careful about leaving live instances alive. You have to explicitly destroy them and de-allocate the memory that it was consuming.

The runtimes of Python, C#, and JavaScript use a garbage-collection mechanism that automatically de-allocates memory used by instances that aren't referenced anymore. The garbage-collection process is a bit more complicated, and each programming language and runtime has specific considerations that should be taken into account to avoid unnecessary memory pressure. However, let's keep our focus on the object's life cycle. In these programming languages, when the runtime detects that you aren't referencing an instance anymore and when a garbage collection occurs, the runtime executes the code specified within the instance's destructor.

Tip

You can use the destructor to perform any necessary cleanup before the object is destroyed and removed from memory. However, take into account that JavaScript doesn't provide you with the possibility to customize a destructor.

For example, think about the following situation. You need to count the number of instances of a specific class that are being kept alive. You can have a variable shared by all the classes. Then, you customize the class constructor to atomically increase the value for the counter, that is, increase the value of the variable shared by all the classes of the same time. Finally, you customize the class destructor to automatically decrease the value for the counter. This way, you can check the value of this variable to know the objects that are being referenced in your application.

主站蜘蛛池模板: 宣汉县| 沙田区| 吴堡县| 上蔡县| 竹北市| 颍上县| 辰溪县| 长武县| 柞水县| 得荣县| 错那县| 呼图壁县| 沭阳县| 莆田市| 常熟市| 石楼县| 灵台县| 理塘县| 九寨沟县| 东明县| 汾西县| 博兴县| 交口县| 海原县| 叶城县| 吉安市| 吉林市| 蓝田县| 忻城县| 上虞市| 连山| 民权县| 达尔| 柘荣县| 民勤县| 舒城县| 通州市| 镇远县| 乌兰浩特市| 乳源| 镶黄旗|