我可以在Groovy中覆盖cast操作符吗? [英] Can I override cast operator in Groovy?

查看:131
本文介绍了我可以在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 ?

这篇关于我可以在Groovy中覆盖cast操作符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆