Groovy and Grails Recipes翻译之旅——面向对象的Groovy(16)
?
4.16.???如何拦截类中不存在的方法?您通过重载methodMissing方法甚至可拦截不存在的方法,如清单4.35。
?
清单4.35 拦截不存在的方法
class MOP{
?def methodMissing(Stringname, args) {
?????? "$name methoddoesn't exist, are you sure you spelled it right?"
??? }
}
MOP mop = new MOP()
assert mop.none() == "none method doesn't exist, are you sureyou spelled it right?"