this的使用

1、使用this调用本类中的属性

 1 class Person{
 2     private String name;
 3     private int age;
 4     public Person(String name,int age){
 5         name=name;
 6         age=age;
 7     }
 8     public String getInfo(){
 9         return "姓名:    "+name+"年龄:    "+age;
10     }
11 }
12 
13 public class ThisDemo{
14     public static void main(String[]  args)
15     {
16         Person per1=new Person("张三",33);
17         System.out.println(per1.getInfo());
18     }
19 }

上述结果说明:现在的构造方法并不能成功把传递进去的值赋值给类中的属性。也就是说,在赋值时属性并不是明确地被指出。实际上name=name;age=age;都是构造方法中的参数。

 1 class Person{
 2     private String name;
 3     private int age;
 4     public Person(String name,int age){
 5         this.name=name;
 6         this.age=age;
 7     }
 8     public String getInfo(){
 9         return "姓名:    "+name+"年龄:    "+age;
10     }
11 }
12 
13 public class ThisDemo{
14     public static void main(String[]  args)
15     {
16         Person per1=new Person("张三",33);
17         System.out.println(per1.getInfo());
18     }
19 }

 2、使用this调用构造方法

 1 class Person{
 2     private String name;
 3     private int age;
 4     
 5         public Person()
 6         {
 7             System.out.println("一个新的Person对象被实例化。    ");
 8         }
 9     
10     public Person(String name,int age)
11     {
12         this();
13         this.name=name;
14         this.age=age;
15     }
16     
17     public String getInfo(){
18         return "姓名:    "+name+"年龄:    "+age;
19     }
20 }
21 
22 public class ThisDemo{
23     public static void main(String[]  args)
24     {
25         Person per1=new Person("张三",33);
26         System.out.println(per1.getInfo());
27     }
28 }

 1 class Person{
 2     private String name;
 3     private int age;
 4     
 5         public Person()
 6         {
 7             System.out.println("一个新的Person对象被实例化。    ");
 8         }
 9     
10     public Person(String name,int age)
11     {
12         //this();
13         this.name=name;
14         this.age=age;
15     }
16     
17     public String getInfo(){
18         return "姓名:    "+name+"年龄:    "+age;
19     }
20 }
21 
22 public class ThisDemo{
23     public static void main(String[]  args)
24     {
25         Person per1=new Person("张三",33);
26         System.out.println(per1.getInfo());
27     }
28 }

 1 class Person{
 2     private String name;
 3     private int age;
 4     
 5         public Person()
 6         {
 7             System.out.println("一个新的Person对象被实例化。    ");
 8         }
 9     
10     public Person(String name,int age)
11     {
12         //this();
13         this.name=name;
14         this.age=age;
15         this();
16     }
17     
18     public String getInfo(){
19         return "姓名:    "+name+"年龄:    "+age;
20     }
21 }
22 
23 public class ThisDemo{
24     public static void main(String[]  args)
25     {
26         Person per1=new Person("张三",33);
27         System.out.println(per1.getInfo());
28     }
29 }

说明构造方法是在实例化对象时被自动调用的,也就是说在类中的所有方法中,只有构造方法是被优先调用的,所以使用this调用构造方法必须也只能放在构造方法的第一行。

 1 class Person{
 2     private String name;
 3     private int age;
 4     
 5         public Person()
 6         {
 7             this.("xh",20);
 8             System.out.println("一个新的Person对象被实例化。    ");
 9         }
10     
11     public Person(String name,int age)
12     {
13         //this();
14         this.name=name;
15         this.age=age;
16         this();
17     }
18     
19     public String getInfo(){
20         return "姓名:    "+name+"年龄:    "+age;
21     }
22 }
23 
24 public class ThisDemo{
25     public static void main(String[]  args)
26     {
27         Person per1=new Person("张三",33);
28         System.out.println(per1.getInfo());
29     }
30 }

说明this调用构造方法时一定要留一个构造方法作为出口,即程序中至少存在一个构造方法不使用this调用其他构造方法。

3、this表示当前对象

 1 class Person{    
 2     public String getInfo(){
 3         System.out.println("Person类:    "+this);
 4         return null;
 5     }
 6 }
 7 
 8 public class ThisDemo{
 9     public static void main(String[]  args)
10     {
11         Person per1=new Person();
12         Person per2=new Person();
13         System.out.println("main:    "+per1);
14         per1.getInfo();
15         
16         System.out.println("main:    "+per2);
17         per2.getInfo();
18     }
19 }

原文地址:https://www.cnblogs.com/xh0102/p/5707759.html