zoukankan      html  css  js  c++  java
  • this的用法【基于C#】

    昨天看一本C#的书,写了有关this的用法。今天想起来,就写一个笔记,当做记忆吧。

    其实,this说到底有3种用法(我所知道的),下面我很粗略的分析一下,如有不正之处,敬请指教:

    1.限定被相似的名称隐藏的成员

    这种情况一般式在使用变量的时候,使用2个相同的变量的时候,这个时候this就很有效果了。比如

    public class FirstThisDemo
        {
            private string name;
            public FirstThisDemo(string name)
            {
                this.name = name;
            }
    
            public void PrintInformation()
            {
                Console.WriteLine("输入的名字为{0}", this.name);
            }
        }
    

      然后再控制台进行对它的调用:

      static void Main(string[] args)
            {
                FirstThisDemo firstdemo = new FirstThisDemo("Lanny");
                firstdemo.PrintInformation();
            }
    

      如你所见,输出的结果是:输入的名字为Lanny

    但是如果你对name的前面不加this的话,比如:

    public FirstThisDemo(string name)
            {
                name = name;
            }
    

      那么对不起,当你在控制台调用的时候就显示不出来。而且还会显示出一个警告来:

                   警告 37 对同一变量进行赋值;是否希望对其他变量赋值? 

         这个就是this在限定成员中作用。

    2.this在索引器的使用

    this的第二个用处是可以在类中进行申明索引器,如果这样的话吗,就可以在类中很好对类中的成员进行遍历。

    索引器使得对象可按照与数组相似的方法进行索引。
     特点如下:

    ☆get 访问器返回值。set 访问器分配值。

    ☆this 关键字用于定义索引器。

    ☆value 关键字用于定义由 set 索引器分配的值。

    ☆索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。

    ☆索引器可被重载。

    ☆索引器可以有多个形参,例如当访问二维数组时

    直接看代码,先看一个简单的

     class MySecondDemo
        {
            private int[] test = new int[10];
            public int this[int index]
            {
                set
                {
                    if (index > 0)
                    {
                        test[index] = value;
                    }
                }
                get
                {
                    if (index > 0 && index < 10)
                    {
                        return test[index];
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
        }
    

      这里的意思是说对数组中的值存入,然后再进行可以想平时array一样,在指定的第几个进行读取。

    MySecondDemo thisDemo = new MySecondDemo();
                for (int i = 0; i < 10; i++)
                {
                    thisDemo[i] = i + 1;
                }
                Console.WriteLine(thisDemo[8]);
    

      上面输出指定的第8个元素,也就是9.

    this的索引器,也可以对class进行索引。比如如下:

    class Person
        {
            public Person()
            { }
            ArrayList lists = new ArrayList();
            private string name;
            private int age;
            private string province;
            public string Name
            {
                set { name = value; }
                get { return name; }
            }
            public int Age
            {
                set { this.age = value; }
                get { return this.age; }
            }
            public string Province
            {
                set { this.province = value; }
                get { return this.province; }
            }
    
            public Person(string name, int age, string province)
            {
                this.name = name;
                this.age = age;
                this.province = province;
            }
            public string this[string name, int age]
            {
                set
                {
                    lists.Add(new Person(name, age, value));
                }
                get
                {
                    foreach (Person person in lists)
                    {
                        if (person.Age == age && person.name == name)
                        {
                            return person.province;
                        }
                    }
                    return "没有这个省份";
                }
            }
    
            /// <summary>
            /// 重载一个索引器
            /// </summary>
            /// <param name="province">传入省份</param>
            /// <returns></returns>
            public ArrayList this[string province]
            {
    
                get
                {
                    ArrayList temp = new ArrayList();
                    // if(lists)
                    foreach (Person person in lists)
                    {
                        if (person.province == province)
                        {
                            temp.Add(person);
                        }
                    }
                    return temp;
                }
            }
        }
    

      上面2个例子就是简单的this索引器的实例。

    2.1 索引器和属性的区别

    下面摘自MSDN的一段比较,MSDN 属性和索引器的区别连接

     3.this在用于构造函数声明

    因为我们在平时对构造函数重载的时候,如果重载很多的话,会非常的麻烦,所以如果用调用this的话,就可以很好的进行处理。

    使用方法:

    可以使用如下的形式来声明实例构造函数:

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

    {

       【语句块】

    }

    其中的this表示该类本身所声明的、形式参数表与『实际参数表』最匹配的另一个实例构造函数,这个构造函数会在执行正在声明的构造函数之前执行。

    例子我就不写了,博客园内有一篇写的很简练的,原文地址 谢谢EvenPeng的劳动成果。

    代码如下:

    public class Person
        {        
            public string personName;
            //定义年龄为可空类型,这样就可以赋予其null值
            public int? personAge;
    
            //下面前三个构造函数都是去调用参数最多的第四个构造函数,只取它们所需要的部分参数即可
            //这样的做法就是this串联构造函数
            public Person():this("",0)
            {
                
            }
    
            public Person(string name):this("evan",null)
            {
                
            }
    
            public Person(int age):this("",20)
            { 
                
            }
    
            public Person(string name, int? age)
            {
                this.personName = name;
                //通过 ?? 判断传入的age是否null值
                //如果属于null值,则赋值100
                this.personAge = age ?? 100;
            }
    
            public void Display()
            {
                Console.WriteLine("Name:{0},Age:{1}\n", personName, personAge);
            }        
        }
    

      然后再控制台调用相关:

    static void Main(string[] args)
        {
            Person per1 = new Person();
            per1.Display();            
    
            Person per2 = new Person(20);
            per2.Display();            
    
            Person per3 = new Person("evan");
            per3.Display();            
    
            Person per4 = new Person("evan", 20);
            per4.Display();
    
            Console.ReadLine();            
         }
    

      这样的做法就是让一个接受参数最多的构造函数做"主构造函数", 且在主构造函数中实现必须的业务逻辑,其余的构造函数只要使用this关键字把传入的参数转发给主构造函数,并且提供必须的其它参数,这样子,我们整个类中需要我们操心的就是那个主构造函数了,其余构造函数基本上可以为空(注意:如果构造函数链中还有实现各自的逻辑,那么实际上是先执行主构造函数的代码,再执行各自逻辑),使用这种做法,真正的工作都交给了一个构造函数,类定义就会更简洁、更易维护、简化了编程任务。

    参考文献:Even Peng 博客:http://www.cnblogs.com/py891021/archive/2009/09/15/1566859.html

                  MSDN.

  • 相关阅读:
    java实验报告三
    学术论文撰写准备事项整理
    mac终端下运行shell脚本
    关于1*1卷积核的理解
    车牌识别项目的准备思路
    快捷键备忘
    跑caffe过程中的备忘
    caffe中全卷积层和全连接层训练参数如何确定
    从零开始足球战术分析
    卷积与反卷积以及步长stride
  • 原文地址:https://www.cnblogs.com/damonlan/p/2196934.html
Copyright © 2011-2022 走看看