码迷,mamicode.com
首页 > Windows程序 > 详细

C# this关键字

时间:2015-04-18 17:24:00      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

 

使用方式之一:

 

this,在构造函数中使用。

 

当使用构造函数的重载时,可使用this关键字。

 

 1         //构造函数-重载
 2          public Student(int id, string name, int age, int math)
 3         {
 4             this.Id = id;
 5             this.Name = name;
 6             this.Age = age;
 7             this.Math = math;
 8         }
 9 
10         //this关键字,具有最少参数的构造器调用具有最多参数的构造器
11         public Student(int id, int math)
12             :this(id,"NULL",0, 100)
13         {
14         }

 

 

当调用 public Student(int id, int math)(命名为构造函数1)构造函数的时候,因为使用了this关键字,所以执行的顺序是,当执行构造函数1的时候,系统会直接把构造函数1中的参数,传递给 public Student(int id, string name, int age, int math)(命名为构造函数2),然后在构造函数2中,把值赋给属性。

 

C# this关键字

标签:

原文地址:http://www.cnblogs.com/KTblog/p/4437643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!