The Java™ Tutorials - Learning the Java Language - Using the this Keyword

本文详细介绍了在Java中如何使用this关键字。this用于在实例方法和构造函数中引用当前对象,特别是在字段被方法参数遮蔽时。示例展示了如何在构造函数中使用this调用同一类的其他构造方法,并解释了显式构造方法调用的规则。
摘要由CSDN通过智能技术生成

Using the this Keyword

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

在实例方法或构造方法里面,this 是对当前对象的引用 —— 即正在调用方法或构造方法的对象。可以使用 this 从实例方法或构造方法中引用当前对象的任何成员。

Using this with a Field 与字段一起使用

The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

使用 this 关键词最常见的原因是字段被方法或构造方法的参数所遮蔽。

For example, the Point class was written like this

例如,Point 类是这样编写的

public class Point {
   
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int a, int b) {
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值