【java】 this 关键字

this 关键字指的是当前调用的对象,如果有 100 个对象,将有 100 个 this 对象指向各个对象 

this 关键字可以使用在: 

➢ 当局部变量和成员变量重名的时候可以使用 this 指定调用成员变量 ;

➢ 通过 this 调用另一个构造方法;

➢ 在构造方法中,如果使用 this 方法调用和其他构造方法,那么 this 语句必须放在第一句。如果不放在第一句,会发生编译错误 

需要注意:this 只能用在构造函数和成员方法内部(声明变量也可以用),static 标识的方法里是不能使用 this 的。

1、当局部变量和成员变量重名的时候可以使用 this 指定调用成员变量 

public class Goods {
    private Integer id;        /*商品id*/
    private String name;       /*商品名称*/
    private Integer amount;    /*商品库存*/
    private Float price;       /*商品单价*/

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", amount=" + amount +
                ", price=" + price +
                '}';
    }
}

通过 this 可以取得当前调用对象,采用 this 明确指定了成员变量的名称,所以就不会赋值到 局部变量,了解狭义和广义上的 javabean 的概念 。

2、通过 this 调用另一个构造方法 

(1)通过 new 调用当前对象的构造函数(这样是不对的) 

class Student {    
    //学号  
    private int id;     
    //姓名  
    private String name;    
    //性别  
    private boolean sex;    
    //地址  
    private String address;    
    //年龄  
    private int age;    
    
    public Student(int id, String name) {
           /*   this.id = id;   
                this.name = name;   
                this.sex=true;   
                this.address="北京";   
                this.age=20;   */   
            
            //调用构造函数,部分值采用默认值   
            new Student(id, name, true, "北京", 20);  
    }

public Student(int id, String name, boolean sex, String address, int age) {   
           this.id = id; 
           this.name = name;   
           this.sex = sex;   
           this.address = address;   
           this.age = age;  
    }

} 

(2)通过 this 调用当前对象的构造函数 

class Student {    
    //学号  private int id;     
    //姓名  private String name;    
    //性别  private boolean sex;    
    //地址  private String address;    
    //年龄  private int age;    
    
    public Student(int id, String name) {
           /*   this.id = id;   
                this.name = name;   
                this.sex=true;   
                this.address="北京";   
                this.age=20;   */   
            
            //采用 this 调用当前对象对象的构造函数,不能采用 new,以上调用时错误的  
            this(id, name, true, "北京", 20);  
    }

public Student(int id, String name, boolean sex, String address, int age) {   
           this.id = id; 
           this.name = name;   
           this.sex = sex;   
           this.address = address;   
           this.age = age;  
    }

} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值