告诉我如何在.net应用中使用构造函数. [英] tell me how to use constructor in .net applcations.

查看:145
本文介绍了告诉我如何在.net应用中使用构造函数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能指导我有关构造函数的真正功能.
我从很多地方进行了研究,但使用时仍然无法完全理解.
请举个例子,说明如何在.net应用程序中使用它们以及何时足够使用它们.

can anyone please guide me about the true functionalty of a constructor.
I studied it from the lots of places but still cannot understand completely when it is used.
Please give some example how we can use them in .net application and when it is sufficient to use them.

推荐答案

每当创建一个实例时,就使用构造函数.课.

因此,每当看到new关键字时,您都在查看对构造函数的调用.它可能是默认构造函数,没有可见代码,但会被调用.

由于C#中的所有内容均来自单个类(object类),因此您在应用程序中使用的所有内容都具有一个构造函数,该构造函数在创建实例时被调用,该实例是字符串,表单或SQL数据库连接.

您如何使用构造函数?设置您的实例,使其在不执行任何其他操作时将起作用.或通过参数设置内部字段.

这是一个很大的主题,并且很难一一涵盖.我建议您问您的导师:他至少能够看到您的反应并再次说明令人困惑的部分.
You use a constructor whenever you create an instance of a class.

So whenever you see the new keyword, you are looking at a call to a constructor. It may be a default constructor, with no visble code, but it will be called.

Since everything in C# is derived from a single class (the object class) everything you use in you application has a constructor which is called when the instance is created, where it is a string, a Form, or a SQL database connection.

What do you do with a constructor? Set up your instance so that it will work if nothing else is done to it. Or set up internal fields via parameters.

This is a big subject, and difficult to cover at a large remove. I would suggest that you ask your tutor: he will at least be able to see your reactions and explain confusing parts again.


它用于初始化对象本身的状态(可以使用传递的参数或默认值). IE.对于开发人员来说,这是一个在实际使用之前将其置于众所周知的状态的机会.
假设您正在编写自己的Stack类.它可能具有tos(堆栈顶部)成员变量. Stack类构造函数应设置tos=0,在构造后将对象置于众所周知的状态(空堆栈).
:)
It is used to initialize the state of the object itself (either using passed parameters or default values). I.e. it is an opportunity, for the developer, to put the object in a well known state, before it is actually used.
Suppose you''re coding your own Stack class. Probably it has a tos (top of the stack) member variable. The Stack class constructor should set tos=0, putting the object in a well know state (empty stack) after contruction.
:)


对于每个类,默认情况下将自动定义一个空的构造函数,除非您手动提供一个构造函数定义.每当创建对象时,类中的构造函数都可用于初始化变量或执行一些基本功能.

例如,您可以搜索google.有很多.
For every class an empty constructor will be defined automatically by default unless you provide a constructor definition manually. Constructor in a class can be used to initialize variables or perfrom some basic functionallity whenever an object is created.

For examples you can search google. There are lots of them.


这篇关于告诉我如何在.net应用中使用构造函数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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