this关键字

表示本类对象的引用,本质是一个对象

每一个普通方法均有一个this,谁调用该方法,this就指向谁

public class ThisDemo {
    public static void main(String[] args) {
        Student3 student3=new Student3();
        student3.show();
    }
}
class Student3{
    String name;
    private int age=20;
    public void setAge(int age){
        this.age=age;
    }
    public int getAge(){
        return this.age;
    }
    public void show(){
        int age=10;
        System.out.println(age);
        System.out.println(this.age);
    }
}
输出结果为  10   20

 

posted @ 2019-10-27 21:31  王迎婧  阅读(105)  评论(0编辑  收藏  举报