为什么在类中使用构造器? [英] Why constuctor is used in a class?
本文介绍了为什么在类中使用构造器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么在类中使用构造函数?
Why constructor is used in a class?
推荐答案
您首先需要了解为什么在面向对象的编程中使用构造函数. >
构造函数不过是方法.
整个想法是准备要使用的新对象(例如,带有对象方法可能需要的参数等).
构造函数可以在子类中继承.
You need to understand why a constructor is used in object-oriented programming in the first place.
A constructor is nothing but a method.
The whole idea is to prepare a new object for use (e.g. with parameters etc that the object methods might need).
A constructor can be inherited in a sub-class.
构造函数创建要在程序中使用的新对象.它也可以用于初始化数据成员.它没有任何返回类型.
Constructor creates the new object for using in a program. It can also be for initializing data members. It does not have any return type.
请参阅这篇写得很好的CP文章 ^ ].
Refer to this nicely written CP article, An Intro to Constructors in C#[^].
这篇关于为什么在类中使用构造器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文