this关键字

1.首先一个用处就是代表当前类的对象。

2.当我们对构造函数进行重载的时候代码如下:

 public class Class1
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Grade { get; set; }
        public int English { get; set; }
        public int Math { get; set; }
        public int Chinese { get; set; }
        public Class1(string name,int age,string grade,int english,int math,int chinese)
        {
            this.Name = name;
            this.Age = age;
            this.Grade = grade;
            this.English = english;
            this.Math = math;
            this.Chinese = chinese;
        }
        public Class1(string name,int english, int math, int chinese)
        {
            this.Name = name;
            this.English = english;
            this.Math = math;
            this.Chinese = chinese;
        }
        public Class1(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
        public Class1()
        {

        }
    }

以上代码完全没有问题,但是不免有些代码冗余的现象。我们可以使用this关键字

 public class Class1
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Grade { get; set; }
        public int English { get; set; }
        public int Math { get; set; }
        public int Chinese { get; set; }
        public Class1(string name,int age,string grade,int english,int math,int chinese)
        {
            this.Name = name;
            this.Age = age;
            this.Grade = grade;
            this.English = english;
            this.Math = math;
            this.Chinese = chinese;
        }
        public Class1(string name,int english, int math, int chinese):this(name,0,"c",english,math,chinese)
        {
            //this.Name = name;
            //this.English = english;
            //this.Math = math;
            //this.Chinese = chinese;
        }
        public Class1(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
        public Class1()
        {

        }

    }

即this的第二个用处为在类中显式的调用本类的构造函数

最新文章

  1. 简述linux同步与异步、阻塞与非阻塞概念以及五种IO模型
  2. SQL语法
  3. 【转】不容忽视的ClassNotFoundException
  4. 【12-JDBC编程】
  5. centos7安装mplayer 错误集锦
  6. web前端工程师校园招聘要求
  7. spring-data-elasticsearch整合elasticsearch
  8. C语言接口的写法(以toyls命令为例)
  9. AFNetworking源码分析
  10. 3TB硬盘的容量已经超出了传统分区标准的支持
  11. Web Api中实现Http方法(Put,Post,Delete)
  12. 用php和imagemagick来处理图片文件的上传和缩放处理
  13. UVa 495 - Fibonacci Freeze
  14. 萌新的IDEA_web开发笔记(未完)
  15. C# Winform开发以及控件开发的需要注意的,被人问怕了,都是基础常识
  16. 今天开始学习模式识别与机器学习Pattern Recognition and Machine Learning (PRML),章节5.1,Neural Networks神经网络-前向网络。
  17. (转载)基于Unity~UGUI的简单UI框架(附UIFramework源码)
  18. html5-Input类型
  19. Spring Boot和Dubbo整合
  20. 每日英语:Got a Case of the Mondays? Blame the Sunday Blues

热门文章

  1. JavaScript 客户端JavaScript之样式表操作(DOM API 提供模块之一)
  2. JAVA-1-HelloWorld
  3. Oracle数据库之创建表空间与用户
  4. 转:Raspberry Pi(树莓派)试用小记
  5. 如何打一手好Log(转)
  6. Leetcode:Largest Number详细题解
  7. BZOJ3391: [Usaco2004 Dec]Tree Cutting网络破坏
  8. N次剩余
  9. js中iframe的用法
  10. poj3237 Tree