例如
class Student{
private int sid=5
public int getsid(){
return this.sid//这里返回就是sid本身
}
public static void main(String[] args){
Student s=new Student()
s.getsid()
}
}
在java中,this关键字有很多种用法。 在java中,这是一个引用当前对象的引用变量。
java this关键字的用法如下:
this关键字可用来引用当前类的实例变量。
this关键字可用于调用当前类方法(隐式)。
this()可以用来调用当前类的构造函数。
this关键字可作为调用方法中的参数传递。
this关键字可作为参数在构造函数调用中传递。
this关键字可用于从方法返回当前类的实例。
建议:如果你是java初学者,只学习 this 关键字的前三个用法就可以了。
更详细可以查询一些教程,例如:https://www.yiibai.com/java/this-keyword.html