首页 > 其他 > 详细

Groovy A simple DSL based on groovy

时间:2015-10-15 12:36:41      阅读:214      评论:0      收藏:0      [点我收藏+]
import groovy.transform.Field;
 
@Field def eat = new Expando()
 
class FriendHadnler {
    def scriptObject
 
    void friend(String name) {
        scriptObject.eat.friend = name
    }
}
 
@Field def friendHadnler = new FriendHadnler(scriptObject : this)
 
void place(String place) {
    eat.place = place
}
 
void time(String time) {
    eat.time = time
}
 
void withFriend(Closure cl) {
    cl.setResolveStrategy(Closure.DELEGATE_FIRST)
    cl.setDelegate(friendHadnler)
    cl.call()
}
 
def eatSth(Closure cl) {
    cl.call()
    println eat.place + ‘ ‘ + eat.time + ‘ with friend ‘ + eat.friend
}
 
eatSth {
    place     ‘meituan tower‘
    time      ‘12:30 pm‘
    withFriend {
        friend ‘han meimei‘
    }
}

  

Groovy A simple DSL based on groovy

原文:http://www.cnblogs.com/lotusJade/p/4881944.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!