Key Difference – Constructorvs驱动器
大多数编程语言支持面向对象的编程(OOP)。是有助于使用软件或程序建模的范式对象。OOP提高了生产率和可维护性。在OOP中,一切都被视为对象。对象是使用类创建或实例化的。构造函数和破坏者是面向对象的编程中的常见术语。本文讨论了构造函数和驱动器之间的区别。构造函数和破坏者是A中的特殊成员功能class。构造函数和破坏者的名称与班级相同,但是驱动器具有tilde(〜)符号。这关键区别between a constructor and destructor is that构造函数用于将内存分配给对象,同时驱动器用于处理对象的内存。
CONTENTS
1。概述和关键差异
2。What is a Constructor
3。什么是破坏者
4。构造函数和驱动器之间的相似之处
5。——构造函数vs Destructo并排比较r in Tabular Form
6.Summary
什么是构造函数?
构造函数是类中的特殊成员函数,可以将内存分配给对象。它可用于为数据成员提供值。创建对象时调用构造函数。它具有与班级名称相同的名称。构造函数不会返回任何值。因此,它不包含返回类型。构造函数也可以接受parameters。具有参数的构造函数称为参数化构造函数。
An example of a constructor is as follows.
公共类矩形{
int长度,宽度;
公共矩形(int p,int q){
长度= p;
宽度= q;
}
public int calculateArea(){
return (length * width);
}
}
According to the above piece of code, the constructor has the same name as the class name. The constructor Rectangle accepts two parameters. They are p and q. The integer value p is assigned to the length. The integer value q is assigned to the width. In the calcualteArea, the multiplication of length and width is calculated to find the area of the rectangle. In the main program, the programmer can create an object of type Rectangle and pass the arguments. e.g. Rectangle rect1= new Rectangle( 2,3). Then, the parameterized constructor is called and assigns the values to the length and width.
When there is a constructor without any parameters, it is called a default constructor. If the programmer does not define a constructor, the default constructor will be invoked. If there is a class as Student and when the programmer creates an object of type Student, the default constructor is called. e.g. Student s1= new Student(); There can be multiple constructors with different parameters and different data types in a class. The appropriate constructor can be called accordingly. Therefore, constructors can be超载。
什么是破坏者?
驱动器是同类中的特殊成员函数。它用于将内存分配为由构造函数创建的对象。当物体被摧毁时,将调用破坏者。它执行了不再需要的清理存储空间。像构造函数一样,驱动器的名称与班级相同。它还包含一个tilde(〜)符号。
A destructor does not return any value. Unlike a constructor, the destructor does not accept any parameters. So, destructor does not support overloading. Declaring a destructor is considered as a good programming practice because it releases the memory space and that space can be used for storing some other objects. The syntax of destructor is similar to ~className() { }. e.g. ~Rectangle() { }; There can only be a single destructor in a class.
构造函数和破坏者之间有什么相似之处?
- 构造函数和破坏者都与对象相关联。
- 构造函数和破坏者都不返回任何值。
- Both constructor and destructor are called automatically.
What is the Difference Between Constructor and Destructor?
构造函数vs destructor |
|
构造函数是类中的特殊成员,用于将内存分配给对象。 | A destructor is a special member of the class that is used to deallocate memory of an object. |
调用方法 | |
创建对象时调用构造函数。 | 当物体被摧毁或删除时,称为攻击器。 |
用法 | |
构造函数用于分配对象的内存。 | 驱动器用于处理对象的内存。 |
参数 | |
构造函数接受参数。 | A destructor does not accepts parameters. |
构造函数和破坏者的数量 | |
这re can be multiple constructors with a different number of parameters and different types of parameters. | 这re can be single destructor in the class. |
执行速度 | |
构造函数的名称与班级名称相同。 | A destructor has the same name as the class name with a tilde (~) symbol. |
超载 | |
构造函数可以超载。 | 破坏者不能超载。 |
概括 -构造函数vs驱动器
OOP是软件开发中的常见范式。它可以简化一个复杂的项目。OOP中使用了构造函数和破坏者。构造函数和破坏者的名称与班级相同,但是驱动器具有一个标志。构造函数和破坏者之间的区别在于,构造函数用于将内存分配给对象,而destuructor用于处理对象的内存。
Download the PDF of Constructor vs Destructor
您可以下载本文的PDF版本,并根据引文注释将其用于离线目的。请在此处下载PDF版本:构造函数和驱动器之间的区别
参考:
1。tutorialspoint.com. “Java Object and Classes.”重点,在这里可用
Leave a Reply