groovy - 我可以在 Groovy 中覆盖 cast 运算符吗?

标签 groovy casting

我需要尽可能类似的东西:

interface Bar { 
    def doSomething()
}

class Foo { // does not implement Bar.

    def doSomethingElse() {
    }

    Bar asBar() { // cast overload
        return new Bar() {
            def doSomething() {
                doSomethingElse()
            }
        }
    }

}

Foo foo = new Foo()
Bar bar = foo as Bar
bar.doSomething()

Groovy 中有这样的东西吗?

最佳答案

您是否尝试过覆盖 Object#asType(Class)方法?

关于groovy - 我可以在 Groovy 中覆盖 cast 运算符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5247648/

相关文章:

security - 不安全使用用户提供的 GString :s in Groovy/Grails

csv - 使用executescript处理器更新csv值在apache-nifi中使用groovy失败

python - 随机 "int is not subscriptable"行为

Java 和 JVM 混淆(如果 Java 可以处理大字符串为什么 groovy 不能?)

java - GroovyScriptEngine : load groovy scripts from subfolder

grails - Groovy:访问父类中的属性

postgresql - 为什么将 JSONb NULL 转换为类型失败,这是一个规范错误?

types - 将IplImage类型IPL_LABEL转换为IplImage类型IPL_DEPTH_8U

c++ - 我应该使用哪个 *_cast 将任意整数类型的指针转​​换为 char 指针?

c++ - 如何在 C++ 中正确转换结构