如果在方法内部调用同一个类的另一个方法,不需使用this,直接调用即可。当前方法中的this引用会自动应用于同一个类中 的其他方法
public class Person{ public void eat(){...} public void action(){ eat(); // eat(); --> this.eat(); 一般省略this关键字 }} 在action内部,可以写成this.eat(); 但是编译器可以自动添加,所以没必要自己添加。 一般只有在需要明确指出对当前对象的引用时才需要使用this关键字 2.区别全局变量和局部变量,引用成员变量 当出现了局部变量和全局变量重名的时候,需要对全局变量和局部变量进行区分,使用this进行区分 this指的是当前调用者 this.变量名 指的是成员变量 this指的是当前调用者,当前调用该方法的对象public class Animal{ public static void main(String[] args){ Person p1 = new Person("zs"); Person p2 = new Person("ww"); System.out.PRintln("p1.name="+p1.name); //zs p1.eat(); //zs在吃东西 p2.eat(); //ww在吃东西 } } class Person{ public String name; public int age; public char gender; public Person(){ } public Person(String name){ this.name = name; } //当出现了局部变量和全局变量重名的时候,需要对全局变量和局部变量进行区分,使用this进行区分 public void eat(){ String name = "ls"; System.out.println(this.name+"在吃东西"); } } 1).在Animal中调用Person的构造方法时,带参数的构造方法public Person(String name)中参数name与成员变量name同名,如果不使用this调用成员变量,则编译器会根据就近原则,认为name=name两个name都是参数列表的name而不是成员变量name,所以在行5中,输出结果为p1.name=null。所以要使用this明确指出调用当前对象的成员变量name:this.name=name,此时行5结果为:p1.name=zs2)在Animal中调用Person的eat()时,当eat()中输出直接调用name时,会就近原则调用eat()中的局部变量name,所以p1.eat()结果为:ls在吃东西;当在eat()中输出用this调用name,此时调用的时成员变量name,所以p1.eat()结果为:zs在吃东西,p2.eat()结果为:ww在吃东西 3.在构造器中调用其他构造器 this(); 调用本类中空的构造方法 this(参数列表); 表示对符合此参数列表的某个构造方法的明确调用 通过this调用其他构造方法,必须让该语句方法在构造方法的第一行,并且该构造方法只能有一个this调用其他构造方法的语句
1 public class Person{2 public void eat(){...}3 public void action(){4 eat(); // eat(); --> this.eat(); 一般省略this关键字5 }6 }public class Animal{ public static void main(String[] args){ Person p = new Person("zs",12,'男'); //两个带参数的构造方法都调用了 } }class Person{ public String name; public int age; public char gender; public Person(){ System.out.println("初始无参构造方法"); } public Person(String name){ System.out.println("初始name构造方法"); this.name = name; } public Person(String name,int age){ this(name); this.age = age; System.out.println("初始name和age构造方法"); } /* //错误的写法:对this的调用必须是构造器中的第一个语句,同一个构造方法中不能出现两个this对其他构造器进行调用 public Person(String name,int age){ this(); this(name); this.age = age; System.out.println("初始name和age构造方法"); } */ public Person(String name,int age,char gender){ this(name,age); this.gender = gender; }} 在上段代码中,Animal中创建Person对象p,调用构造方法public Person(String name,int age,char gender),此方法通过this(name,age);调用public Person(String name,int age),此构造方法又调用public Person(String name)构造方法结果:System.out.println("初始name和age构造方法"); System.out.println("初始name构造方法"); 好处:避免重复代码
public class Flower{ int petalCount = 0; //花瓣数量 String s = "initial value"; Flower(int petals){ petalCount = petals; System.out.println("初始化petals的构造方法,petalCount="+petalCount); } Flower(String ss){ System.out.println("初始化ss的构造方法,ss="+s); } Flower(String s,int petals){ this(petals); //this(s); //同一个构造方法不能this两个 this.s = s; //this.s指的是成员变量s System.out.println("String & int 参数列表"); } Flower(){ this("hi",47); //this(s); //同一个构造方法不能this两个 this.s = s; //this.s指的是成员变量s System.out.println("无参数构造方法"); } public void printPetalCount(){ //this(11); //不能在其他方法中使用this()调用构造器,只能在构造器中使用 System.out.println("petalCount="+petalCount+" s="s); } public static void main(String[] args){ Flower flower = new Flower(); x.printPetalCount(); } /* 输出结果: 初始化petals的构造方法,petalCount=47 String & int 参数列表 无参数构造方法 petalCount=47 s=hi */ } 构造器Flower(String s,int petals)表明:可以用this调用一个构造器,但却不能调用两个,并且必须将构造器调用放置于第一行最起始处,否则会报错在构造器Flower(String s,int petals)中,参数s的名称和数据成员s的名字相同,所以会产生歧义,使用this来区分
printPetalCount方法表明,除构造器外,编译器禁止在其他任何方法中调用构造器
新闻热点
疑难解答