猿问

C# this. 是什么意思?

public Rectangular(double d)
{
this.width = d;
this.height = d;
}
public Rectangular(double width, double height)
{
this.width = width;
this.height = height;
}
就是这里面的this 有什么用的啊

jeck猫
浏览 1005回答 3
3回答

拉莫斯之舞

就是指代当前的对象,public Rectangular(double d){this.width = d;this.height = d;}这个this就是指当前这个Rectangular。这样在你实例化这个类的时候,调用不同的Rectangular的width和height属性就互不影响了。

繁星点点滴滴

因为构造函数的参数和对象的属性是同名的,如果你在第二个函数中删除了this,那就产生了歧义了,程序认为等号左边的width和height仍然是参数。只有加了this.以后,程序才能明白你是要给对象属性赋值。 

largeQ

this代表调用这个函数的类的对象如果你的程序里面是这么调用函数的:obj.Rectangular(800,600);那么,函数里面的this就是obj
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答