think in java笔记:this关键字

来源:互联网 发布:rimworld mac下载 编辑:程序博客网 时间:2024/05/16 18:44

think in java笔记:this关键字

The this keyword—which can be used only inside a
non-static method—produces the reference to the object that the method has been called
for. You can treat the reference just like any other object reference. Keep in mind that if
you’re calling a method of your class from within another method of your class, you don’t
need to use this . You simply call the method. The current this reference is automatically
used for the other method.
The this keyword is also useful for passing the current object to another method

译:
This关键字,只可以用在非静态方法中,指向当前方法所属的对象。你可以把这个对象引用当作正常的对象来对待,但是你要记住,如果在一个方法中需要调用另外一个此对象的方法,不需要加this,可以直接调用方法。当前对象的引用会自动应用到方法上。
同时this关键字也是很有用的,在调用其它方法是,将本身对象当作参数传入进去。
还有一种用法,是在构造函数中调用构造函数时,会用到this。

ps:

  1. 构造函数使用this的时候,只能在第一行调用,并且只能调用一次
  2. 调用构造函数只能是构造函数,其他函数不可以
0 0