mun*_*nna 3 c# constructor-chaining
在以下代码中:
public class A
{
public A():this(null){}
public A(string b){/*code here*/}
}
Run Code Online (Sandbox Code Playgroud)
第一个构造函数有什么用?
Geo*_*off 26
第一个构造函数传递null给第二个构造函数的参数b.
因此,如果你打电话new A(),它将与呼叫相同new A(null)
当你有一个带参数的构造函数时
public A(string b){ /* code here */ }
public A():this("") { } //default
Run Code Online (Sandbox Code Playgroud)
默认构造函数实际上使用""作为参数调用"参数构造函数".您正在传递参数.这样做是为了避免两次编写相同的代码
| 归档时间: |
|
| 查看次数: |
409 次 |
| 最近记录: |