什么是类的构造函数? [英] what is constructor of a class?

查看:285
本文介绍了什么是类的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好frinds ..

我对Constructor有点困惑。

怎么样..为什么......以及何时使用它??

解决方案

如果你开始学习VB.NET,你会在使用VB.NET的面向对象编程的第一章中学到这一点,如果你去那里,甚至MSDN也会这样做!



使用构造函数和析构函数 [ ^ ],选择VB.NET作为您的语言,您将看到构造函数和析构函数的实例。



现在,关于您的问题,构造函数只是在类中定义的函数,当您的类被实例化时执行。我们使用然后创建新对象,它们可以在创建对象时用于添加我们自己的逻辑,例如:



1.初始化字段。

2.授权和身份验证。

3.后端管理,比如调用另一个API等。



阅读提供的文档,它包含有关此主题的大量信息。


在基于类的面向对象编程中,类中的构造函数(缩写:ctor)是一种特殊类型的子例程调用创建一个对象。它准备使用新对象,通常接受构造函数用来设置所需成员变量的参数。



另一个定义 -

构造函数是面向对象编程中类或结构的特殊方法,用于初始化该类型的对象。构造函数是一种实例方法,通常与类具有相同的名称,可以使用设置memb的值对象的值,默认值或用户定义的值。



在C#中,如果未提供构造函数,则会创建默认值。

hello frinds..
I am little bit of confused about Constructor.
how it is.. why.. and when use it..?

解决方案

If you start learning VB.NET, you are taught this in the first chapter of Object-oriented programming using VB.NET, even MSDN would do that if you went there!

Using Constructors and Destructors[^], select VB.NET as your language and you would be shown live examples of constructors and destructors.

Now, as to your questions, a constructor is simply a function defined in the class, that gets executed when your class is instantiated. We use then we are creating new objects, they can be used to add our own logic while creating the objects such as:

1. Initializing the fields.
2. Authorization and authentication.
3. Backend management, like a call to another API or so on.

Read the documentation provided, it contains much information on this topic.


"In class-based object-oriented programming, a constructor (abbreviation: ctor) in a class is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables."

Another definition -
"A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values."

In C#, if a constructor is not provided, a default one is created.


这篇关于什么是类的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆