Skip to content

Instantly share code, notes, and snippets.

@PandyYang
Created April 27, 2018 13:56
Show Gist options
  • Select an option

  • Save PandyYang/00c9d6c886ff1b96aaa2ed42b9a1fdd3 to your computer and use it in GitHub Desktop.

Select an option

Save PandyYang/00c9d6c886ff1b96aaa2ed42b9a1fdd3 to your computer and use it in GitHub Desktop.

Revisions

  1. PandyYang created this gist Apr 27, 2018.
    21 changes: 21 additions & 0 deletions ThisInConstructor.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    package fkjy;

    /**
    * 如果构造器中有一个和成员变量重名的局部变量,又必须在构造器中访问这个被覆盖的局部变量
    * 则必须使用this前缀
    * 如果没有前缀 则调用的是成员变量
    * @author Pandy
    * @date 2018/4/27 21:46
    */
    public class ThisInConstructor {
    public int foo = 8;
    public ThisInConstructor(){
    int foo = 0;
    //this代表构造器正在初始化的对象
    //该构造器正在初始化的对象的foo成员变量设为6
    //this.foo = 6;
    }
    public static void main(String[] args){
    System.out.println( new ThisInConstructor().foo);
    }
    }