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

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.

主站蜘蛛池模板: 东港市| 米易县| 汤原县| 武陟县| 青州市| 阳朔县| 清流县| 藁城市| 双峰县| 阜平县| 衡南县| 同仁县| 扎鲁特旗| 河源市| 拉孜县| 桃源县| 黄大仙区| 蒙山县| 历史| 高碑店市| 华宁县| 通州区| 塔城市| 鄯善县| 登封市| 贵港市| 湖北省| 自治县| 榕江县| 思茅市| 渭源县| 张北县| 米林县| 三台县| 肥城市| 临朐县| 青冈县| 前郭尔| 嘉黎县| 河源市| 山西省|