dynamicmembertest.groovy

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

GROOVY
40
字号

class DynamicMemberTest extends GroovyTestCase {
  def aTestMethod(o){o}
  def aProperty
  
  public void testGStringMethodCall(){
    def name = "aTestMethod"
    assert this."$name"(1) == 1
    assert this."${name}"(2) == 2
    assert "$name"(3) == 3
    assert "${name}"(4) == 4
    name = "TestMethod"
    assert this.("a"+"TestMethod")(5) == 5
    assert this.("a"+name)(6) == 6
  }
  
  public void testGStringPropertyAccess(){
    def name = "aProperty"
    this.aProperty = "foo"
    assert this."$name" == "foo"
    assert this."${name}" == "foo"
    assert "$name" == "aProperty"
    assert "${name}" == "aProperty"
  }
  
  public void testStringMethodCallAndAttributeAccess() {
    this.aProperty = "String"
    assert this."aProperty" == "String"
    assert this."aTestMethod"("String") == "String"
    assert "aTestMethod"("String") == "String"
  }
  
  public void testDynamicAttributeAccess() {
    this.aProperty = "tada"
    def name = "aProperty"
    assert this.@"$name" == "tada"
    assert this.@"${name}" == "tada"
  }
  
}

⌨️ 快捷键说明

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