c#中的construcor初始化程序 [英] construcor initializer in c#

查看:83
本文介绍了c#中的construcor初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

谁能告诉我它们之间有什么区别?

public Time2(int h,int m,int s):this(h,m ,s)机智没有这个关键字

public Time2(int h,int m,int s)





Time2 time = new Time2(2,30,45);

Hi friends,
Could anyone tell me what difference is between those?
public Time2(int h, int m, int s) : this(h, m, s) wit without this keyword
public Time2(int h, int m, int s)


Time2 time = new Time2(2, 30, 45);

推荐答案

它不会像你发布它的方式那样工作,因为两个构造函数具有相同的签名。稍微更改的示例:
It won't work the way you posted it because both constructors are of the same signature. A slightly changed example:
class Time2
{
    // Constructor with many parameters
    public Time2(int hours, int minutes, int seconds){
        // Implementation
    }

    // Constructor with few parameters
    public Time2(int hours):this(hours, 0, 0)
    {
        // Implementation
    }
}

class Example
{
    private void ExampleMethod()
    {
        Time2 firstTime = new Time2(2, 30, 12);
        Time2 secondTime = new Time2(24);
        Time2 thirdTime = new Time2(24, 0, 0);
    }
}

这里有两个编译器可以通过签名区分的构造函数。这意味着,它们有不同的参数列表。



第二个构造函数只接受一个参数。它调用第一个构造函数,转发给出的参数并将其余部分设置为零。

因此在示例中, secondTime thirdTime 使用不同的构造函数,但保持相同的值。

Here you have two constructors that the compiler can distinguish by their signatures. That means, they have different parameter lists.

The second constructor only takes one parameter. It calls the first constructor, forwards what parameters were given and sets the rest to zero.
Therefore in the example, secondTime and thirdTime use different constructors, but are holding identical values.


在.NET中有没有参数的构造函数,带参数的构造函数,如您所知,您可以拥有多个构造函数,每个构造函数都有唯一的参数清单。



在运行时,.NET编译器通过匹配调用的参数列表(调用构造函数的代码)来决定调用哪个构造函数可用的构造函数。



因此,什么是唯一参数列表:



1 。参数列表中类型的差异:这对我们大多数人来说都是直观的。



2.参数中相同类型的位置差异list:那可能想要利用的东西你的代码;为什么有两个构造函数如下:
In .NET there are Constructors without parameters, Constructors with parameters, and you can, as you know, have multiple Constructors each of which has a unique parameter list.

At run-time the .NET compiler "decides" which Constructor to call by matching the parameter list of the invocation (the code that calls the Constructor) to the possible Constructors available.

So, what qualifies as a "unique" parameter list:

1. a difference in the Types in the parameter list: that's intuitive for most of us.

2. a difference in the position of the (same) Types in the parameter list: that's something you probably don't want to exploit in your code; why have two Constructors like:
public Class1(int x, string y) {}
public Class1(string y, int x) {}

只是为了允许两个不同的使用相同参数的顺序?



遵循带有冒号和关键字this的构造函数的参数列表的右括号的特殊语法,可选地遵循通过括号中的变量列表,允许您将创建类(或结构)的控制流重定向到已定义的其他构造函数。

Just to allow two different orderings of using the same parameters ?

The special syntax of following the closing parenthesis of the parameter list of a Constructor with a colon and the keyword "this," optionally followed by a list of variables in parentheses, allows you to redirect the flow-of-control of your creation of a Class (or Struct) to other Constructors you have defined.

public Class1() {}

public Class1(int x, string y) : this() {}

将在之前调用无参数的Class1构造函数它执行自身内部的任何代码。



就在昨天,CP成员ProgramFox在回答QA问题时向我展示了你甚至可以从无参数重定向执行构造函数的构造函数,其参数作为设置默认值的方式:

Is going to call the parameterless Class1 Constructor before it executes any code inside itself.

Just yesterday, the CP member ProgramFox, in response to a QA question here, showed me that you can even redirect execution from the parameterless Constructor to a Constructor with parameters as a way of setting default values:

public Class1() : this(0, String.Empty)

public Class1(int x, string y)

如果你在代码中写了Class1 newClass1 = new Class1();



然后无参数构造函数将在构造函数中使用参数执行代码,然后在其自己的主体中执行代码。请注意,在本示例的带有参数的构造函数中,我们删除了表达式:this()...如果我们没有删除那么你会得到StackOverflow错误,因为两个构造函数会继续调用自己。



这个无参数的构造函数示例强制执行带参数的构造函数,我猜,这很罕见。



最后,好像你没有足够的工具用于为各种目的创建构造函数:你可以在Constructor参数列表中使用可选参数;例如:

If you wrote in your code Class1 newClass1 = new Class1();

Then the parameterless constructor would execute code in the Constructor with parameters before it executed code in its own body. Notice that in the Constructor with parameters in this example, that we removed the expression ": this()" ... if we didn't remove that you would get a StackOverflow error because the two Constructors would keep calling themselves.

This example of the parameterless Constructor forcing the execution of a Constructor with parameters is, I would guess, quite rare.

Finally, as if you didn't have enough tools to use in creating Constructors for every purpose: you can use optional parameters in Constructor parameter lists; for example:

public Class1(string y, int x, bool z1, bool z2, long l = 123456789, string s = "what ?"){}





请注意,在参数列表的末尾有两个参数l和s,每个参数后跟一个等号和一个默认值。



现在你可以调用以下任何一个:



Note that here, at the end of the parameter list are two parameters "l" and "s" that are each followed by an equal-sign and a default value.

Now you could call any of:

// both optional parameters omitted
Class1 myClass1 = new Class1("wow", 1, true, true);

// both optional parameters supplied
Class1 myClass1 = new Class1("wow", 1, true, true, 987654321, "whoa");

// one optional parameter supplied, one optional parameter omitted
Class1 myClass1 = new Class1("wow", 1, true, true, "whoa");

// using special named parameter: notation to allow entering parameters in any order
Class1 myClass1 = new Class1(s: "wow", z2:false, l: 987654321, x:100, y:"where ?", z1: false);

所有这些自由都令人陶醉,但在实践中,大多数时候你可能会创建多个构造函数来实现很好的理由:你需要在你创建的类的实例中获得不同类型的数据......在不同的情况下。



确保你理解参数顺序 - 和.NET编译器匹配,以确定调用哪个构造函数,以及und理解使用:this(...)指令是最重要的。

All this "freedom" can be intoxicating, but, in practice, most of the time you probably will create multiple Constructors for a good reason: you need to get different sorts of data into the instance of the Class you create ... in different circumstances.

Making sure you understand the parameter order- and type- matching the .NET compiler does in order to determine which Constructor is invoked, and also understanding the use of the ": this(...)" directive are most important.


嗯,公平地说它看起来毫无意义。但是正在发生的事情是,Time2类有多个构造函数。



第一行是将h,m和s的值传递给不同的构造函数在类中。

然而,看看你输入的内容将永远不会工作,永远不会编译,因为Time2的两个构造函数是相同的。
Well, to be fair it looks pretty pointless. But the jist of what is going on is that the Time2 class has multiple constructors.

The first line is passing the values of h, m and s to a different constructor in the class.
However, looking at what you have typed this would never work and would never compile because the two constructors for Time2 are identical.


这篇关于c#中的construcor初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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