Какой результат выполнения данной программы ?

public class Test {
    public static void main(String[] args) {
        TestClone t1 = new TestClone();
        TestClone t2 = (TestClone) t1.clone();
        for(int i = 0; i < t2.arr.length; i++) {
            t2.arr[i] = (t2.arr.length - i);
        }
        System.out.println(t1.arr.equals(t2.arr));
    }
}

class TestClone implements Cloneable {
    int arr[] = {1, 2, 3, 4, 5};
    public Object clone() {
        try {
            return super.clone();
        } catch(CloneNotSupportedException e) {
            System.out.println("Клонирование не разрешено");
            return this;
        }
    }
}
Explanation
Если клонируемый объект содержит ссылочную переменную, то в клоне эта переменная будет ссылаться на тот же объект.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes