this是什么?
- this就是一个变量,可以用在方法中,拿到当前对象使用
this的执行原理
- 方法先进入方法区
- 栈内存先声明变量
- 变量接受堆内存中new的对象
- 对象根据类的地址找到方法区的方法
- 输出
this的应用场景
package com.badidu.thisdemo;
public class Student {
public void printThis() {
System.out.println(this);
}
double score;
public void printPass(double score) {
if (this.score > score) {
System.out.println("恭喜");
} else {
System.out.println("落榜");
}
}
}
package com.badidu.thisdemo;
public class Test {
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1);
s1.printThis();
System.out.println("----------------");
Student s2 = new Student();
System.out.println(s2);
s2.printThis();
Student s3 = new Student();
s3.score = 325;
s3.printPass(250);
}
}