spreaddottest.groovy

来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· GROOVY 代码 · 共 85 行

GROOVY
85
字号
/*
 * SpreadDotTest.groovy
 *
 * @author  Pilho Kim
 */


/**
 * Test for the spread dot operator "*.".
 *
 * For an example,
 *          list*.property
 * means
 *          list.collect { it?.property }
 */
public class SpreadDotTest extends GroovyTestCase {
    public void testSpreadDot() {
        def m1 = ["a":1, "b":2]
        def m2 = ["a":11, "b":22]
        def m3 = ["a":111, "b":222]
        def x = [m1,m2,m3]
        println x*.a
        println x*."a"
        assert x == [m1, m2, m3]

        def m4 = null
        x << m4
        println x*.a
        println x*."a"
        assert x == [m1, m2, m3, null]

        def d = new SpreadDotDemo()
        x << d
        println x*."a"
        assert x == [m1, m2, m3, null, d]

        def y = new SpreadDotDemo2()
        println y."a"
        println y.a

        x << y
        println x*."a"
        assert x == [m1, m2, m3, null, d, y]
    }

    public void testSpreadDot2() {
        def a = new SpreadDotDemo()
        def b = new SpreadDotDemo2()
        def x = [a, b]

        println ([a,b]*.fnB("1"))
        assert [a,b]*.fnB("1") == [a.fnB("1"), b.fnB("1")]

        println ([a,b]*.fnB())
        assert [a,b]*.fnB() == [a.fnB(), b.fnB()]
    }
}

class SpreadDotDemo {
    public java.util.Date getA() {
        return new Date()
    }
    public String fnB() {
        return "bb"
    }
    public String fnB(String m) {
        return "BB$m"
    }
}

class SpreadDotDemo2 {
    public String getAttribute(String key) {
        return "Attribute $key"
    }
    public String get(String key) {
        return getAttribute("Get $key")
    }
    public String fnB() {
        return "cc"
    }
    public String fnB(String m) {
        return "CC$m"
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?