为什么refrence p的值也改变了?(解释为什么堆的bc?)

时间:2016-06-16 16:11:20

标签: java class object reference

我看了很多网站,但我找不到答案。我的问题是为什么refrence p的值也改变了?即使我没有改变那些?这是我的代码和thak yu的解释。

public static void main(String[] args) {

    Person p = new Person();
    Person q = new Person();
    p.name="Sara";
    p.birthyear= 1989;

    q=p;
    System.out.println("Name is "+q.name+" and birthyear is "+q.birthyear);

    q.name ="Hanna";
    q.birthyear = 1991;

    System.out.println("Name is "+p.name+" and birthyear is "+p.birthyear);
}

}

class Person { 
String name;
String surname;
int birthyear;

}

0 个答案:

没有答案