8this关键字

this

指向对象自己的一个指针,this可以表示自己本身的对象。

作用

1)、代表当前类的对象
2)、在类当中显示的调用本类的构造函数 :this

在类显示调用本类构造函数

public Person(string name, int age, int chinese, int math, int english)
        {
            this.Name = name;
            this.Age = age;
            this.Chinese = chinese;
            this.Math = math;
            this.Engllish = english;
        }


        public Person(string name,int age):this(name,age,0,0,0)
        {
			//在实例化的时候执行这个构造函数的话,他会去执行上面那个构造函数,执行完后再再执行当前这个构造函数。
        }

在类中输出属性时最好用this来输出,不加的话,如果方法里有和属性同名的变量,就会把变量当作结果输出。

posted @ 2021-10-03 16:11  青仙  阅读(39)  评论(0编辑  收藏  举报