`

Groovy 中的 with

 
阅读更多

in Java 

 

// PrintIndependenceDay.java

import java.util.Calendar;
import java.util.Date;

public class PrintIndependenceDay {

  public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.MONTH, Calendar.JULY);
    calendar.set(Calendar.DATE, 4);
    calendar.set(Calendar.YEAR, 1776);
    Date time = calendar.getTime();
    System.out.println(time);
  }
}

 in Groovy:

 

 

// PrintIndependenceDay.groovy

def calendar = Calendar.instance
calendar.with {
  clear()
  set MONTH, JULY
  set DATE, 4
  set YEAR, 1776
  println time
}

 每个 Groovy闭包有个与其关联的delegate(代理人).

 

 

// define a closure
def myClosure = {
  // call a method that does not exist
  append 'Jeff'
  append ' was here.'
}

// assign a delegate to the closure
def sb = new StringBuffer()
myClosure.delegate = sb

// execute the closure
myClosure()

assert 'Jeff was here.' == sb.toString()

 换一种写法:

 

 

def closure = {
  clear()
  set MONTH, JULY
  set DATE, 4
  set YEAR, 1776
  println time
}
def calendar = Calendar.instance
closure.delegate = calendar
closure()

 Another bit of info that is missing here is the strategy that a closure uses to decide when to send method calls to the delegate. Each Groovy closure has a resolveStrategy associated with it. This property determines how/if the delegate comes in to play. The 4 possible values for the resolveStrategy are OWNER_FIRST, DELEGATE_FIRST, OWNER_ONLY and DELEGATE_ONLY (all constants defined in groovy.lang.Closure). The default is OWNER_FIRST. Consider the owner to be the "this" wherever the closure is defined. Here is a simple example...

 

 

 

class ResolutionTest {
    
    def append(arg) {
        println "you called the append method and passed ${arg}"
    }
    
    def doIt() {
        def closure = {
            append 'Jeff was here.'
        }
        
        def buffer = new StringBuffer()
        
        closure.delegate = buffer
        
        // the append method in this ResolutionTest
        // will be called because resolveStrategy is
        // OWNER_FIRST (the default)
        closure()
        
        // give the delegate first crack at method
        // calls made inside the closure
        closure.resolveStrategy = Closure.DELEGATE_FIRST
        
        // the append method on buffer will
        // be called because the delegate gets
        // first crack at the call to append()
        closure()
    }
    
    static void main(String[] a) {
        new ResolutionTest().doIt()
    }
}

 运行结果:

you called the append method and passed Jeff was here.

 

 来源:

http://java.dzone.com/news/getting-groovy-with-with

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Groovy in action

    Readers are presented with rich and detailed examples illustrating Groovy's enhancements to Java, including, # How to Work with Builders and the GDK, # Database programming with Groovy, Groovy in ...

    [Groovy] Making Java Groovy 英文版

    Making Java Groovy is a practical handbook for developers who want to blend Groovy into their day to day work with Java It starts by introducing the key differences between Java and Groovy and how you...

    groovy-2.0.8-with-eclipse-plugin-2.7.2.part3

    groovy-2.0.8-with-eclipse-plugin-2.7.2.part3

    Groovy插件

    为了在Eclipse中使用Groovy进行开发,你需要安装Groovy插件。下面我们将详细解释如何在Eclipse中安装Groovy插件。 首先,打开Eclipse IDE。在菜单栏上选择“Help”,然后点击“Eclipse Marketplace”。这是Eclipse...

    groovy包及相关学习资料

    - **GDK(Groovy Development Kit)**:Groovy扩展了Java的标准库,提供了许多方便的方法和类,如`each`、`with`等,简化了日常编程工作。 - **Meta-programming(元编程)**:Groovy支持元编程,允许在运行时修改...

    Groovy and Grails Recipes(清晰PDF)

    Getting Started with Groovy** - **介绍**: 本章主要介绍了如何开始使用Groovy编程语言。对于初次接触Groovy的新手来说,这是一个很好的起点。 - **核心知识点**: - 安装Groovy环境 - Groovy脚本的基本语法 - ...

    Groovy User Guide

    它与 Java 具有很好的兼容性,可以在现有的 Java 应用程序中轻松地集成 Groovy 脚本。Groovy 的官方参考文档《Groovy User Guide》提供了关于如何使用 Groovy 编写脚本和应用程序的全面指南。 #### 官方文档概览 *...

    Agile Development with Groovy and Grails.pdf

    在《Agile Development with Groovy and Grails》这本著作中,作者Christopher M. Judd(Judd Solutions, LLC 的总裁兼顾问)详细介绍了如何运用Groovy和Grails框架进行敏捷软件开发。此书不仅适合那些对敏捷方法论...

    Groovy 2.3.6 windows

    首先,Groovy 2.3系列带来了对Java 7的全面支持,这允许开发者利用Java的新特性,如try-with-resources语句和多线程fork/join框架。同时,它也保持了与Java 6的兼容性,使得在旧环境下仍能顺畅运行。 Groovy的动态...

    groovy-1.5.6-installer

    of Groovy: “Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like Python, Ruby and Smalltalk, making them available to Java developers using...

    Groovy 学习笔记

    2. **Ant with Groovy**: Groovy可以作为Ant任务的语言,使Ant脚本更加简洁和强大。 **五、Groovy的其他应用** 1. **脚本编写**: Groovy的简洁语法使其成为编写自动化脚本的理想选择,如系统管理、测试和持续集成...

    GROOVY入门经典

    书中的内容不仅涵盖了Groovy语言的基础知识,还深入介绍了如何使用Groovy语言创建高级应用程序,比如利用Spring框架和Cloudscape/Derby关系型数据库管理系统实现数据持久性,以及如何开发模板和Web应用程序。...

    [Groovy入门]第五讲.将流程控制语句与方法重构为闭包

    此外,Groovy的`Closures`类提供了一些有用的方法,如`call`、`use`和`with`,帮助我们更好地利用闭包。例如,`use`方法可以确保资源在使用后正确关闭,这对于处理I/O操作特别有用: ```groovy new File('example....

    apache groovy 2.4.8

    3. **GDK (Groovy Development Kit)**:Groovy扩展了Java标准库,提供了一系列方便的方法,如`with`、`each`、`collect`等,这些都极大地提升了代码的可读性和效率。 4. ** Grape**: Groovy的依赖管理工具Grape允许...

Global site tag (gtag.js) - Google Analytics