比较类似术语之间的差异

之间的区别

Home / 技术 / 它 / 编程 /构造函数和驱动器之间的区别

构造函数和驱动器之间的区别

2018年2月12日发表Lithmee

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.

构造函数和驱动器之间的区别

图01:构造函数和驱动器

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.”重点,在这里可用

Related posts:

之间的区别Logical Address and Physical Address之间的区别Logical Address and Physical Address 汇编器和编译器之间的区别 继承和组成之间的区别 平行计算和分布式计算之间的差异平行计算和分布式计算之间的差异 C#中的类和结构之间的区别C#中的类和结构之间的区别

提交以下:编程Tagged With:比较构造函数和破坏者差异,,,,构造函数,,,,构造函数和破坏者的相似性,,,,构造函数定义,,,,构造函数调用,,,,构造函数号,,,,构造函数超载,,,,构造函数参数,,,,构造函数Speed,,,,构造函数vs destructor,,,,驱动器,,,,驱动器定义,,,,驱动器Invoking,,,,破坏者编号,,,,驱动器超载,,,,破坏者参数,,,,破坏者速度

关于作者:Lithmee

LithmeeMandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a ReplyCancel reply

您的电子邮件地址不会被公开。必需的地方已做标记*

Request Article

精选文章

冠状病毒和冷症状之间的差异

冠状病毒和冷症状之间的差异

之间的区别Coronavirus and SARS

之间的区别Coronavirus and SARS

冠状病毒和流感的差异

冠状病毒和流感的差异

冠状病毒和covid 19之间的差异

冠状病毒和covid 19之间的差异

You May Like

原位和原位保护之间的差异

原位和原位保护之间的差异

玉米面粉和玉米粉之间的区别

可分割和不可分配的形容词之间的差异

可分割和不可分配的形容词之间的差异

机密性和隐私之间的差异

机密性和隐私之间的差异

廉价和昂贵的HDMI电缆之间的区别

最新的帖子

  • 水素荨麻疹和水蛋白瘙痒之间有什么区别
  • 收敛剂和碳粉有什么区别
  • 食道炎和巴雷特食管有什么区别
  • What is the Difference Between Alcohol Ink and Resin Dye
  • 甲状旁腺功能亢进和甲状腺功能亢进之间有什么区别
  • 珠光和虹彩有什么区别
  • Home
  • 空缺
  • 关于
  • Request Article
  • Contact Us

版权所有©2010-2018之间的区别。版权所有。使用条款and Privacy Policy:Legal。