Groovy and Grails Recipes翻译之旅——面向对象的Groovy(16)

 

4.16.   如何拦截类中不存在的方法?

您通过重载methodMissing方法甚至可拦截不存在的方法,如清单4.35

 

清单4.35 拦截不存在的方法

class MOP{

 def methodMissing(String name, args) {

       "$name method doesn't exist, are you sure you spelled it right?"

    }

}

MOP mop = new MOP()

assert mop.none() == "none method doesn't exist, are you sure you spelled it right?"

你可能感兴趣的:(grails,groovy)