Bu yazimda javada clonelamayi anlatmaya calisacagim. Clonelamak var olan objemizin aynisindan bir tane daha olusturmaktir.
Java'da Object classinda clone() methodu ile objemizin aynisindan bir tane daha yaratabiliriz. Ama her obje clonelanmaz. Clonelayabilmek icin classlarimizin Cloneable interface'ini implement etmeleri gerekir.
Default olarak clone() methodu shallow copy yapar. Bizim deep copy yapabilmemiz icin clone() methodunu override etmemiz gerekir.
Oncelikle shallow copy e bakalim.
SHALLOW COPY
Shallow copy de objenin aynisi uzerinden kopyalama yapilir.
class Course{
String subject1;
String subject2;
String subject3;
public Course(String subject1, String subject2, String subject3) {
this.subject1 = subject1;
this.subject2 = subject2;
this.subject3 = subject3;
}
}
class Student implements Cloneable{
int id;
String name;
Course course;
public Student(int id, String name, Course course) {
this.id = id;
this.name = name;
this.course = course;
}
protected Object clone() throws CloneNotSupportedException{
return super.clone();
}
}
public class ShallowCopy {
public static void main(String[] args) {
Course science = new Course("Physics", "Chemistry", "Biology");
Student student1 = new Student(1, "Joe", science);
Student student2 = null;
try {
student2 = (Student) student1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println(student1.course.subject3);
student2.course.subject3 = "Math";
System.out.println(student1.course.subject3);
}
}
OutputBiology
Math
Gordugumuz gibi student2 nin yani clone objemizin degerini degistirdigimizde orjinal objemizinde degeri degisti.
DEEP COPY
Deep clone yaptigimizda ise clone() methodunu override etmemiz gerekir. Deep te de shallow gibi objenin aynisi olusturulur ancak 2 objenin referansi birbirinden farklidir.
Bu durumda birinde yapilan degisiklik digerini etkilemez.
class Course implements Cloneable{
String subject1;
String subject2;
String subject3;
public Course(String subject1, String subject2, String subject3) {
this.subject1 = subject1;
this.subject2 = subject2;
this.subject3 = subject3;
}
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Student implements Cloneable{
int id;
String name;
Course course;
public Student(int id, String name, Course course) {
this.id = id;
this.name = name;
this.course = course;
}
protected Object clone() throws CloneNotSupportedException {
Student student = (Student) super.clone();
student.course = (Course) course.clone();
return student;
}
}
public class DeepClone {
public static void main(String[] args) {
Course science = new Course("Physics", "Chemistry", "Biology");
Student student1 = new Student(1, "Joe", science);
Student student2 = null;
try {
student2 = (Student) student1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println(student1.course.subject3);
student2.course.subject3 = "Math";
System.out.println(student1.course.subject3);
}
}
Output:Biology
Biology
Genel olarak bu sekilde.
Hiç yorum yok:
Yorum Gönder