关闭

17.10.6 Optional instance constructor paramet

650人阅读 评论(0) 收藏 举报
[Note: The this(?) form of constructor initializer is commonly used in
conjunction with overloading to
implement optional instance constructor parameters. In the example
class Text
{
public Text(): this(0, 0, null) {}
public Text(int x, int y): this(x, y, null) {}
public Text(int x, int y, string s) {
// Actual constructor implementation
}
}
the first two instance constructors merely provide the default values for
the missing arguments. Both use a
this(?) constructor initializer to invoke the third instance constructor,
which actually does the work of
initializing the new instance. The effect is that of optional constructor
parameters:
Text t1 = new Text(); // Same as Text(0, 0, null)
Text t2 = new Text(5, 10); // Same as Text(5, 10, null)
Text t3 = new Text(5, 20, "Hello");
end note]
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:570165次
    • 积分:11516
    • 等级:
    • 排名:第1392名
    • 原创:565篇
    • 转载:0篇
    • 译文:0篇
    • 评论:50条
    文章分类