Java中类对象作为类的成员变量,传递的是对象的地址

//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所共享的;

[如有不足,畅所欲言,谢谢点评]

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值