Skip to content

Instantly share code, notes, and snippets.

@PandyYang
Created April 27, 2018 13:56
Show Gist options
  • Save PandyYang/00c9d6c886ff1b96aaa2ed42b9a1fdd3 to your computer and use it in GitHub Desktop.
Save PandyYang/00c9d6c886ff1b96aaa2ed42b9a1fdd3 to your computer and use it in GitHub Desktop.
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment