AJPFX简述Java中this关键字的使用

Java中this关键字的使用主要有两处:
  1、构造方法
    this指的是调用构造方法进行初始化的对象。

//有参构造public Human(String name, int age) { this(); //调用无参构造 //this(name); //调用有参构造(参数为name的构造方法) this.name = name; this.age= age;}


  2、普通方法(非静态方法)
    this指的是调用该方法的对象。
//普通方法public void setName(String name) { this.name = name;}

  为什么在构造器、普通方法中能使用this关键字?
  ——this是隐式参数,在调用方法时系统自动传递一个this参数(代表调用的对象的引用),只不过是隐式传递的(super类似)。

  为什么静态方法中不能使用this关键字?
  ——静态方法有可能不是被对象调用的(如:被类直接调用),因此this没有对象可引用

原文地址:https://www.cnblogs.com/AJPFX/p/10894276.html