Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Groovy中尝试求值时缺少MethodException_Groovy_Scope - Fatal编程技术网

在Groovy中尝试求值时缺少MethodException

在Groovy中尝试求值时缺少MethodException,groovy,scope,Groovy,Scope,我试图在类方法调用期间动态评估另一个函数的结果。但是,对评估范围存在争议 class A { private String a public A() { a = 5 } public whatIsA() { return a } public func() { return "\\${whatIsA()}" } public test() { return Ev

我试图在类方法调用期间动态评估另一个函数的结果。但是,对评估范围存在争议

class A {
    private String a
    public A() {
        a = 5
    }

    public whatIsA() {
        return a
    }


    public func() {
        return "\\${whatIsA()}"
    }

    public test() {
        return Eval.me("\"\${func()}\"")
    }
}
def a = new A()
a.test()



Exception thrown: groovy.lang.MissingMethodException: No signature of method: Script1.func() is applicable for argument types: () values: {}

groovy.lang.MissingMethodException: No signature of method: Script1.func() is applicable for argument types: () values: {}
    at Script1.run(Script1.groovy:1)
    at A.test(Script7:17)
    at Script7.run(Script7:22)

如何将类实例的作用域传递到Eval脚本中?

我认为以当前形式不可能。。。我能想到的最好方法是使用
Eval.x
如下:

public test() {
    return Eval.x( this, '"${x.func()}"' )
}
或者将表达式包装在闭包中(然后可以在闭包中更改委托)

如果您不希望将其作为脚本运行,而是作为已编译的应用程序运行,那么您可能可以使用和/或执行一些路由。(请参阅:)

试试这个

class A {
    private String a
    public A() {
        a = 5
    }

    public whatIsA() {
        return a
    }

    public func() {
        return "${whatIsA()} + 5"
    }

    public test() {
        return Eval.me(func())
    }
}
def a = new A()
a.test()
class A {
    private String a
    public A() {
        a = 5
    }

    public whatIsA() {
        return a
    }

    public func() {
        return "${whatIsA()} + 5"
    }

    public test() {
        return Eval.me(func())
    }
}
def a = new A()
a.test()