当前位置: 动力学知识库 > 问答 > 编程问答 >

casting - Can I override cast operator in Groovy?

问题描述:

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

Is there something like this in Groovy?

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: