// 浅拷贝测试 System.out.println("Before modification:"); System.out.println("Original: " + person1); // 输出 John lives in New York System.out.println("Shallow Copy: " + shallowCopyPerson); // 输出 John lives in New York
// 修改引用类型字段的值 address.city = "San Francisco"; System.out.println("After modification:"); System.out.println("Original: " + person1); // 输出 John lives in San Francisco System.out.println("Shallow Copy: " + shallowCopyPerson); // 输出 John lives in San Francisco
// 深拷贝测试 System.out.println("Deep Copy: " + deepCopyPerson); // 输出 John lives in New York } }
3. 运行结果
运行上述代码时,输出如下:
1 2 3 4 5 6 7 8 9
Before modification: Original:JohnlivesinNewYork Shallow Copy:JohnlivesinNewYork
After modification: Original:JohnlivesinSanFrancisco Shallow Copy:JohnlivesinSanFrancisco