告诉我构造函数重载 [英] tell me about constructor overload

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

问题描述

任何人都可以告诉我在什么情况下可以使用构造函数重载?????

can anybody tell me under what situation the constructor overloading can be used?????

推荐答案

构造函数不仅仅是一个自动调用的类中的方法当一个实例/对象创建该类时...



无法使用cn创建该类的对象来构造函数..



通常构造函数用于在创建该类的对象时设置类成员的初始值。



重载意味着您​​可以创建一个或多个然后记住一个类中的构造函数但是要记住:



: - 如果你创建了多个具有相同参数数量的构造函数,参数数据类型必须是不同的。



: - 如果你创建了多个具有相同参数数量的构造函数,参数序列必须是不同的。



: - 数字如果你创建了多个构造函数,参数必须是不同的。



示例代码:



Constructor is nothing just a method in a class which called automatically when an instance/object will create of the class...

Constructor can not be called withcout creating object of that class..

Generally Constructor are used to set initial values of class members while creating object of that class.

overloading means you can create one or more then one Constructor in a class but remember :

:- parameter datatype must be diffrent if u r creating more then one Constructor with same number of parameter.

:- parameter sequence must be diffrent if u r creating more then one Constructor with same number of parameter.

:- Number of parameter must be diffrent if u r creating more then one Constructor.

Sample Code :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for Class1
/// </summary>
public class MyClass
{
    // DEFAULT CONSTRUCTOR
    public MyClass()
    {
        
    }

    // PARAMETERISED CONSTRUCTOR --> STRING
    public MyClass(string value1, string value2)
    {

    }

    // PARAMETERISED CONSTRUCTOR --> STRING
    public MyClass(string value1, string value2, string value2)
    {

    }

    public MyClass(int value1, int value2)
    {

    }

    public MyClass(string value1, int value2)
    {

    }

    public MyClass(int value1, string value2)
    {

    }
}


推荐此链接



C#中的构造函数介绍 [ ^ ]



它声明:

Refer this link

An Intro to Constructors in C#[^]

It states that:
引用:

C#支持构造函数的重载,这意味着我们可以拥有具有不同参数集的构造函数。所以,我们的课程可以是这样的:

C# supports overloading of constructors, that means, we can have constructors with different sets of parameters. So, our class can be like this:




public class mySampleClass
{
    public mySampleClass()
    {
        // This is the no parameter constructor method.
        // First Constructor
    }

    public mySampleClass(int Age)
    {
        // This is the constructor with one parameter.
        // Second Constructor

    }

    public mySampleClass(int Age, string Name)
    {
        // This is the constructor with two parameters.
        // Third Constructor
    }

    // rest of the class members goes here.
}




Quote:

那么请注意,现在调用构造函数取决于方式你实例化对象。例如:

Well, note here that call to the constructor now depends on the way you instantiate the object. For example:




mySampleClass obj = new mySampleClass()
mySampleClass obj = new mySampleClass(12)



我认为现在很清楚。



问候..:)


I think it is now clear for you.

Regards..:)


告诉我们什么?当你需要多个构造函数时。



让我告诉你,重载是一个虚构的术语,常常引起混淆。这种语言功能并不值得一个特殊的名称。我怀疑如果没有名字存在,那么混淆就会少得多。它只是意味着不同的方法可以具有相同的名称,因此在可能的情况下,编译器必须解决通过实际参数调用的方法。这包括构造函数,根据语法,它们都与它们的类或结构名称具有相同的名称。



我希望你不需要解释为什么一个类或者结构可能需要多个方法(过载或不重载)。为什么应该询问有关构造函数的相同问题?还有方法。



-SA
Tell what? When you need more than one constructor.

Let me tell you that "overload" is a fictional term which often cause confusion. This language feature does not really deserve a special name. I suspect if no name existed, there would be much less confusion. It simply means that different methods can have the same name, so the compiler has to resolve which one to call by actual parameter passed, in cases when it's possible. This includes constructors, which are, by syntax, all have the same name as their class or structure name.

I hope you don't need explanation why a class or structure may need more than one method ("overloaded" or not). Why the same question should be asked about constructors? There are also methods.

—SA


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

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