//1:创建一个BirthDate类:
package cn.dqlai.day10.demo01;
public class BirthDate {
private int year;
private int month;
private int day;
public BirthDate() {
}
public BirthDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "BirthDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
}
//2:创建一个Person类,其成员变量包含一个BirthDate类对象
package cn.dqlai.day10.demo01;
public class Person {
private String name;
private char sex;
private int age;
private BirthDate birthDate;//类对象作为类的成员变量
public Person() {
}
public Person(String name, char sex, int age, BirthDate birthDate) {
this.name = name;
this.sex = sex;
this.age = age;
this.birthDate = birthDate;//引用数据类型传递的是地址
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public BirthDate getBirthDate() {
return birthDate;
}
public void setBirthDate(BirthDate birthDate) {
this.birthDate = birthDate;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex=" + sex +
", age=" + age +
", birthDate=" + birthDate +
'}';
}
}
//3:创建测试类Test01
package cn.dqlai.day10.demo01;
public class Test01 {
public static void main(String[] args) {
//1:定义一个BirthDate对象---date
BirthDate date = new BirthDate(1998,4,27);
System.out.println(date);//输出结果:BirthDate{year=1998, month=4, day=27}
//2:创建一个Person对象---p1,并用1中的date对象初始化p1
Person p1 = new Person("dqlai",'男',22,date);
System.out.println(p1);//输出结果:Person{name='dqlai', sex=男, age=22, birthDate=BirthDate{year=1998, month=4, day=27}}
//p1的成员变量birthDate和构造方法中传递的date属性相同
p1.getBirthDate().setYear(1996);//修改p1中的date对象的属性year:1998--->1996
System.out.println(p1);//输出结果:Person{name='dqlai', sex=男, age=22, birthDate=BirthDate{year=1996, month=4, day=27}}
System.out.println(date);//输出结果:BirthDate{year=1996, month=4, day=27}
//1中的date对象的属性和p1中的birthDate对象的year属性同样发生了变化--->说明p1中birthDate对象的改变会导致1中date对象的改变
//3:创建另一个Person类对象---p2,并用1中的date对象初始化p2
Person p2 = new Person("wl",'女',22,date);
System.out.println(p2);//输出结果:Person{name='wl', sex=女, age=22, birthDate=BirthDate{year=1996, month=4, day=27}}
p2的成员变量birthDate和构造方法中传递的date属性相同
p2.getBirthDate().setMonth(6);//修改p1中的date对象的属性month:4--->6
System.out.println(p2);//输出结果:Person{name='wl', sex=女, age=22, birthDate=BirthDate{year=1996, month=6, day=27}}
System.out.println(date);//输出结果:BirthDate{year=1996, month=6, day=27}
1中的date对象的属性和p1中的birthDate对象的month属性同样发生了变化--->说明p2中birthDate对象的改变会导致1中date对象的改变
}
}
//总结:1:Person类对象p1在构造时,其成员变量birthDate接收的是date对象的地址值,若Person类中的Birthdate对象改变,则类对象date也会对应的改变;
2:两个Person类对象p1,p2在构造时都是传递date对象的地址值,所以p1,p2中成员变量Birthdate对象的改变都会引起date对象数据的改变;
3:可以简单的理解:date对象是p1,p2所共享的;
[如有不足,畅所欲言,谢谢点评]