首页 > Windows开发 > 详细

C# this关键字

时间:2015-04-18 17:23:42      阅读:301      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!