引用修改笔记

public class DtoTest {
    private myData myData;

    public DtoTest() {
    }

    public DtoTest(com.gahui.test.test.myData myData) {
        this.myData = myData;
    }

    public com.gahui.test.test.myData getMyData() {
        return myData;
    }

    public void setMyData(com.gahui.test.test.myData myData) {
        this.myData = myData;
    }

    @Override
    public String toString() {
        return "DtoTest{" +
                "myData=" + myData +
                '}';
    }

    public static void main(String[] args) {
        DtoTest test = new DtoTest();
        myData data = new myData();
        test.setMyData(data);
        System.out.println("before set -> "+test);
        data.setId(1);
        data.setName("test");
        System.out.println("after set -> "+test);
    }
}


class myData{
    private int id;
    private String name;

    public myData() {
    }

    public myData(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "myData{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

代码运行输出结果:

引用修改笔记_第1张图片

笔记:

这里其实就是地址引用,只要地址对应的实例被修改,取到的值都是最新修改的值。

你可能感兴趣的:(9257--Java)