zoukankan      html  css  js  c++  java
  • 学习this关键字

    在学习MSDN的过程中加入一点自己的理解:

    先学习类实例使用this


    以下是 this 的常用用途:

    • 限定被相似的名称隐藏的成员,例如:

      public Employee(string name, string alias)
      {
          this.name = name;//此处的this.name即代表字段name,而非参数name
          this.alias = alias;
      }
    • 声明索引器,例如:
      public int this [int param]
      {
          get { return array[param];  }
          set { array[param] = value; }
      }/*这个之后单独开一页学习下索引器,索引器可以看做是一种带有参数的特殊属性,而且索引器的名称必须是关键字this还是很简单的。*/
    • 将对象作为参数传递到其他方法,例如:

      CalcTax(this);
      下面引用MSDN的官方示例并添加一些注释:
      // keywords_this.cs
      // this example
      using System;
      class Employee
      {
          private string name;
          private string alias;
          private decimal salary = 3000.00m;//此处的m不能省略否则编译器会视为double类型

          // Constructor:  
          public Employee(string name, string alias)
          {
              // Use this to qualify the fields, name and alias:
              this.name = name; //此处this.name非参数而是字段name
              this.alias = alias;//此处this.alias非参数而是字段alais  
             }

        // Printing method:
        public void printEmployee()
        {
            Console.WriteLine("Name: {0}\nAlias: {1}", name, alias);
            // Passing the object to the CalcTax method by using this:
            Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));/*此处的this即代表Employee类实例,
                       在下面的Main函数中 Tax.CalcTax(this))实为调用Tax.CalcTax(E1));*/   
        }
        public decimal Salary
        {
            get { return salary; }
        }
    }
    class Tax
    {
        public static decimal CalcTax(Employee E)//将对象作为参数传递到CalcTax方法中
        {
            return 0.08m * E.Salary;//此处使用该对象E的Salary字段
        }
    }

    class MainClass
    {
        static void Main()
        {
            // Create objects:
            Employee E1 = new Employee("Mingda Pan", "mpan");

            // Display results:
            E1.printEmployee();
        }
    }

    同时当this用于构造函数事,它总是检查this的实参表匹配情况,先执行与其实参表最匹配的一个构造函数


    使用this的构造函数形式如下:

    【访问修饰符】 【类名】(『形式参数表』):this(『实际参数表』)
    {
         【语句块】
    }


    例如:
     
    using System;

    namespace test1
    {
        class test
        {
            //下面我声明两个参数数目和类型不同的构造函数
            public test(string str1, int num)
            {
                Console.WriteLine("use test(string str1, int num) Constructor");
            }
            public test(int num):this("haha",2)//这个构造函数我用this声明
            {
                {
                    Console.WriteLine("use test( int num) Constructor");
                }
            }
            //现在我们调用测试
            public static void Main()
            {
                test contcorthis = new test(10);// 此时this的实参test(string str1, int num)与匹配
                                                //应该先调用该构造函数
                Console.ReadKey();
            }
        }
    }

    它的输出结果如下:

    use test(string str1, int num) Constructor

    use test( int num) Constructor


  • 相关阅读:
    对进程空间的认识
    自己实现智能指针
    实现一个string类
    常见问题总结
    pong game using ncurses
    知识梳理
    经典算法回顾
    自己实现more命令
    表的垂直拆分和水平拆分-zz
    MySQL索引原理及慢查询优化-zz
  • 原文地址:https://www.cnblogs.com/rohelm/p/2384122.html
Copyright © 2011-2022 走看看