import groovy.transform.AutoClone
import groovy.transform.ToString
@AutoClone
class Course {
String name
Date date
Teacher teacher
}
@AutoClone
@ToString
class Teacher {
String name
}
def mrhaki =
new Teacher(name: 'mrhaki')
def course =
new Course(
name: 'Groovy 101',
date: new Date() + 10,
teacher: mrhaki)
// We make a deep copy.
def secondCourse = course.clone()
assert secondCourse != course
assert !secondCourse.is(course)
assert secondCourse.teacher != course.teacher
println "secondCourse.teacher >>> " +secondCourse.teacher
println "course.teacher >>> " +course.teacher
// Change property on cloned instance.
secondCourse.name = 'Groovy 101 2nd edition'
println "Cloned instance value after change >>>"+ secondCourse.name
assert secondCourse.name == 'Groovy 101 2nd edition'
println "Original value >>> "+course.name
assert course.name == 'Groovy 101'
No comments:
Post a Comment