定义一个类似拷贝构造函数的构造函数 Class1 obj1 = new Class1(); // you get the first obj // Do something on obj1 Class1 obj2 = new Class1(obj1); //Define Class1(Class1 obj) to copy the obj // Do something on obj2, and obj1 will not be changedOr Class1 provide a method Clone(), then Class1 obj1 = new Class1(); // you get the first obj // Do something on obj1 Class1 obj2 = obj1.Clone(); // you get the second obj, not just a ref to obj1 // Do something on obj2, and obj1 will not be changed
像int x 这种就是值引用。
和C++是一样的
Class1 obj1 = new Class1(); // you get the first obj
// Do something on obj1
Class1 obj2 = new Class1(obj1); //Define Class1(Class1 obj) to copy the obj
// Do something on obj2, and obj1 will not be changedOr Class1 provide a method Clone(), then
Class1 obj1 = new Class1(); // you get the first obj
// Do something on obj1
Class1 obj2 = obj1.Clone(); // you get the second obj, not just a ref to obj1
// Do something on obj2, and obj1 will not be changed