//闭包委托策略
class Student {
String name
def pretty = { "My name is ${name}" }
String toString() {
pretty.call()
}
}
class Teacher {
String name1
}
def stu = new Student(name: "jimmy")
def tea = new Teacher(name: "qq")
stu.pretty.delegate = tea
stu.pretty.resolveStrategy = Closure.DELEGATE_FIRST //My name is qq
println stu.toString()
网友评论