this和static关键字、拓展方法

this:代表的是调用者本身即调用者的当前对象。

特点:

1.区别成员字段和局部变量

2.调用其它构造函数

  class Student
  {  //字段
     public string name;
     public int age;
     public Student()
     {
         Console.WriteLine("执行默认构造函数了");
     }
     //构造函数的局部变量和字段相同,且作用域有重叠,所以需要有所区分。其实不区分也可以,但是有时候会出小问题。谁调用,this就代表他。
     public Student(string name, int age):this() //调用此函数之前执行默认构造函数
     {
         this.name = name;
         this.age = age;
         Console.WriteLine("执行有参构造函数了");
     }  
  }
 internal class Mainclus
 {
     public  static void Main(string[] args)
     {
       //student调用了Studnet(name,age)构造函数,那么构造函数了的this就代表他。
       Student student = new Student("ldd",18);

     }
 }


static:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值