读书人

Groovy and Grails Recipes通译之旅

发布时间: 2012-12-20 09:53:21 作者: rapoo

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?"

读书人网 >网络基础

热点推荐