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

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.

主站蜘蛛池模板: 铁力市| 镶黄旗| 满城县| 岳西县| 鞍山市| 大邑县| 射洪县| 阆中市| 泰宁县| 田东县| 罗甸县| 三台县| 万源市| 纳雍县| 徐州市| 合水县| 武川县| 合川市| 新巴尔虎左旗| 上犹县| 营山县| 集贤县| 平谷区| 犍为县| 湘潭县| 固始县| 云和县| 景泰县| 左权县| 会昌县| 施甸县| 疏附县| 常山县| 宜宾市| 长兴县| 镇远县| 盘锦市| 榆中县| 苏尼特左旗| 新竹市| 武清区|