1、使用this调用本类中的属性
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 ThisDemo02 { 14 public static void main(String[] args) { 15 Person per1 = new Person("zhangsan", 30); 16 System.out.println(per1.getInfo()); 17 } 18 19 }
输出结果为:zhangsan;;;;30
2、使用this调用构造方法
如果一个类中有多个构造方法,也可以利用this关键字互相调用;
注意:构造方法是在实例化对象时被自动调用的,也就是说在类中的所有方法中,只有构造方法是被优先调用的,所以使用this调用构造方法必须也只有放在构造方法的第一行。
1 class Person{ 2 private String name; 3 private int age; 4 public Person(){ 5 System.out.println("一个新的Person对象被实例化。"); 6 } 7 public Person(String name,int age){ 8 this(); 9 this.name = name; 10 this.age = age; 11 } 12 public String getInfo(){ 13 return name + ";;;;" + age; 14 } 15 } 16 17 public class ThisDemo02 { 18 public static void main(String[] args) { 19 Person per1 = new Person("zhangsan", 30); 20 System.out.println(per1.getInfo()); 21 } 22 }
输出结果为:
一个新的Person对象被实例化。
zhangsan;;;;30
3、this表示当前对象
1 class Person{ 2 public String getInfo(){ 3 System.out.println("Person类" + this); 4 return null; 5 } 6 } 7 8 public class ThisDemo02 { 9 public static void main(String[] args) { 10 Person per1 = new Person(); 11 Person per2 = new Person(); 12 System.out.println("main方法" + per1); 13 per1.getInfo(); 14 System.out.println("....................." ); 15 System.out.println("main方法" + per2); 16 per2.getInfo(); 17 } 18 }
输出结果为:
main方法thisdemo.Person@1db9742
Person类thisdemo.Person@1db9742
.....................
main方法thisdemo.Person@106d69c
Person类thisdemo.Person@106d69c
4、判断对象是否相等
方法一、
1 class Person{ 2 private String name; 3 private int age; 4 public Person(String name,int age){ 5 this.setName(name); 6 this.setAge(age); 7 } 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public int getAge() { 15 return age; 16 } 17 public void setAge(int age) { 18 this.age = age; 19 } 20 21 } 22 23 public class ThisDemo02 { 24 public static void main(String[] args) { 25 Person per1 = new Person("zhangsan",30); 26 Person per2 = new Person("zhangsan",30); 27 if(per1.getName().equals(per2.getName()) && per2.getAge() == per2.getAge()){ 28 System.out.println("两个对象相等"); 29 }else{ 30 System.out.println("两个对象不相等"); 31 } 32 } 33 }
输出结果为:两个对象相等
方法二
1 class Person{ 2 private String name; 3 private int age; 4 public Person(String name,int age){ 5 this.setName(name); 6 this.setAge(age); 7 } 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public int getAge() { 15 return age; 16 } 17 public void setAge(int age) { 18 this.age = age; 19 } 20 public boolean compare(Person per){ 21 Person p1 = this; 22 Person p2 = per; 23 24 if(p1 == p2){ 25 return true; 26 } 27 if(p1.name.equals(p2.name) && (p1.age == p2.age)){ 28 return true; 29 }else{ 30 return false; 31 } 32 33 } 34 } 35 36 public class ThisDemo02 { 37 public static void main(String[] args) { 38 Person per1 = new Person("zhangsan",30); 39 Person per2 = new Person("zhangsan",3); 40 if(per1.compare(per2)){ 41 System.out.println("两个对象相等"); 42 }else{ 43 System.out.println("两个对象不相等........."); 44 } 45 } 46 }
输出结果为:两个对象不相等.........
5、
1 public class Tester { 2 int var; 3 Tester(double var) { 4 this.var = (int) var; 5 } 6 Tester(int var) { 7 this("hello"); 8 } 9 Tester(String s) { 10 this(); 11 System.out.println(s); 12 } 13 public Tester() { 14 System.out.println("googbay"); 15 } 16 public static void main(String[] args) { 17 Tester t = new Tester(5); 18 } 19 }
输出结果为:
googbay
hello