think in java笔记:this关键字

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值