学校排行榜

(0)

投一票

C#中this关键字 - 构造函数重载调用 官网

this关键字还能在构造函数里调用同一个类的其他构造函数,这被称作构造函数重载调用。 public class Rectangle{ public int width; public int height; // 构造函数1 public Rectangle() : this(1, 1) { } // 构造函数2 public Rectangle(int width, int height) { this.width = width; this.height = height; }}// 调用示例class Program{ static void Main() { Rectangle r = new Rectangle(); Console.WriteLine(r.width); // 输出: 1 Console.WriteLine(r.height); // 输出: 1 }} 注意:构造函数重载调用时,this调用必须是构造函数的第一条语句。
专业
评论
新闻
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。