拷贝对象和原始对象的引用类型引用同一个对象。

    1. public class ShallowCloneExample implements Cloneable {
    2. private int[] arr;
    3. public ShallowCloneExample() {
    4. arr = new int[10];
    5. for (int i = 0; i < arr.length; i++) {
    6. arr[i] = i;
    7. }
    8. }
    9. public void set(int index, int value) {
    10. arr[index] = value;
    11. }
    12. public int get(int index) {
    13. return arr[index];
    14. }
    15. @Override
    16. protected ShallowCloneExample clone() throws CloneNotSupportedException {
    17. return (ShallowCloneExample) super.clone();
    18. }
    19. }
    20. ShallowCloneExample e1 = new ShallowCloneExample();
    21. ShallowCloneExample e2 = null;
    22. try {
    23. e2 = e1.clone();
    24. } catch (CloneNotSupportedException e) {
    25. e.printStackTrace();
    26. }
    27. e1.set(2, 222);
    28. System.out.println(e2.get(2)); // 222