我可以在Groovy中覆盖cast操作符吗?

问题描述:

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

I need something as similar as possible to this:

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()

您尝试覆写 Object#asType(Class) 方法?

Have you tried overriding Object#asType(Class) method ?