java this关键字

特点:this表示当前对象。

当前对象  ←→  当前正在调用实例成员的对象

换言之:谁调用了方法,谁就是当前对象。

 

什么时候使用this关键字呢?

方法间的相互调用;

this.字段;

构造器中相互调用,但是此时this([参数])必须写在构造方法第一行。

this不能用在static修饰的方法里和static修饰的代码块里;

 

Eg:构造方法中的this.name = name;

 

posted @ 2019-07-04 11:13  樊伟胜  阅读(174)  评论(0编辑  收藏  举报