this关键字代表当前对象的引用,用于访问成员变量、方法和构造函数:访问成员变量:this.memberName调用成员方法:this.methodName调用构造函数:this(arguments)

this在Java中的用法
什么是this?
this关键字代表当前对象的引用。它用于访问当前对象的方法、变量和构造函数。
语法
立即学习“Java免费学习笔记(深入)”;
this.memberName
其中,memberName是可以访问的成员(方法、变量或构造函数)。
随缘网络PHP企业网站管理系统V2.0正式发布,该企业网站管理系统采用PHP+MYSQL编写,界面色调风格延续之前1.0版管理系统简洁浅蓝色风格,稍有所变动。变更分类树形目录方式采用jquery库,产品,文章三级无限分类。希望大家能够喜欢。系统中难免有些小问题,希望大家在使用中有什么问题可到本站论坛提出,我们将总结各问题后给予修正并升级。本站再次声明对于免费版系列系统本站不提供QQ电话等技术咨询服
用法
1. 访问成员变量:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}2. 调用成员方法:
public class Calculator {
public int add(int a, int b) {
return this.sum(a, b);
}
private int sum(int a, int b) {
return a + b;
}
}3. 调用构造函数:
public class Animal {
private String type;
public Animal(String type) {
this.type = type;
}
public Animal(String type, int age) {
this(type); // 调用有参构造函数
}
}注意事项
- this关键字只能在非静态上下文中使用,例如实例方法或构造函数中。
- 在使用this之前,必须对对象进行实例化。
- this用于区分当前对象与其他对象。










