构造函数间调用:
描述Person对象:
package android.java.oop08;
// 描述Person对象
public class Person {
public String name;
public int age;
public Person() {
/**
* 注意:所有构造方法中的第一行(隐式的) 是执行super();
* 如果一旦写了 this() this(值) super() super(值) 隐式的 第一行 将不再执行了
*/
this("张三");
}
public Person(String name) {
/**
* 注意:所有构造方法中的第一行(隐式的) 是执行super();
* 如果一旦写了 this() this(值) super() super(值) 隐式的 第一行 将不再执行了
*/
this(99);
this.name = name;
// 注意:不能写在 构造方法操作的 下面,否则报错
// this(99);
}
public Person(int age) {
/**
* 注意:所有构造方法中的第一行(隐式的) 是执行super();
* 如果一旦写了 this() this(值) super() super(值) 隐式的 第一行 将不再执行了
*/
this("赵六", age);
this.age = age;
// 注意:不能写在 构造方法操作的 下面,否则报错
// this("赵六", 98);
}
public Person(String name, int age) {
/**
* 现在没有写 his() this(值) super() super(值)
* 所以隐式的 super(); 会执行
*/
this.name = name;
this.age = age;
}
}
main测试方法:
package android.java.oop08;
public class Demo01 {
public static void main(String[] args) {
// 实例化 Person 对象
Person person = new Person();
// 打印值
System.out.println("name:" + person.name + " age:" + person.age) ;
}
}
执行结果:

描述Cat对象:
package android.java.oop08;
// 描述Cat对象
public class Cat {
/**
* 不定义构造方法,默认有一个隐式的构造函数 例如:
* public Cat() {
* ...
* ...
* ...
* }
*/
private double money;
private int age;
public void setMoney(double money) {
this.money = money;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"money=" + money +
", age=" + age +
'}';
}
}
main测试方法:
package android.java.oop08;
public class Demo02 {
public static void main(String[] args) {
// 实例化Cat对象
Cat cat = new Cat();
cat.setMoney(9080938.98);
cat.setAge(99);
/**
* 通过实例化的Cat对象引用地址 去 调用Cat的toString()方法
*/
System.out.println(cat.toString());
}
}
执行结果:

对应以上Cat 案例的内存图:
this内存图:

从以上图进行分析:
this 就是 当前 实例化 对象的 对象引用地址
经过以上图分析:来验证一下就明白了:
Cat:
package android.java.oop08;
// 描述Cat对象
public class Cat {
/**
* 不定义构造方法,默认有一个隐式的构造函数 例如:
* public Cat() {
* ...
* ...
* ...
* }
*/
/**
* 得到当前对象实例化的this
*/
public Cat getThis() {
return this;
}
}
main测试方法:
package android.java.oop08;
public class Demo02 {
public static void main(String[] args) {
// 实例化Cat对象
Cat cat = new Cat();
boolean isItEqual = cat.getThis() == cat;
System.out.println("this 是否等于 Cat cat :" + isItEqual);
}
}
执行结果:
