【问题标题】:Call a method from a Groovy map transformed to a class with asType从转换为具有 asType 的类的 Groovy 映射中调用方法
【发布时间】:2016-01-15 06:02:27
【问题描述】:

是否可以像中那样调用methodTwo()

def map = [methodOne: { return "${methodTwo()}" }, methodTwo: { return "Hey" }] as MyInterface

groovy 在运行时找不到methodTwo(似乎它正在定义映射的类中搜索其定义)

【问题讨论】:

  • 发布MyInterface 代码和您遇到的错误。

标签: methods groovy interface closures


【解决方案1】:

你可以调用之前声明map变量的方法,然后引用它:

interface MyInterface {
    def methodOne()
    def methodTwo()
}

def map
map = [
    methodOne: { return "${map.methodTwo()}" }, 
    methodTwo: { return "Hey" }
] as MyInterface

assert map.methodOne() == "Hey"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 2016-02-25
    • 2011-02-18
    相关资源
    最近更新 更多