Groovy Mixins 能否满足抽象类要求?
我有三个 Groovy 类:M、A 和 A。 B. B 作为 M 的混合和抽象类 A 的扩展而存在:
class M {
def foo = 11
def bar = 12
}
abstract class A {
abstract foo
}
@Mixin(M)
class B extends A {
}
def b = new B()
print "${b.foo}\n"
print "${b.bar}\n"
尝试运行它会导致 Groovy 抱怨: 非抽象类中不能有抽象方法。类“B”必须声明为抽象,或者必须实现方法“void setFoo(java.lang.Object)”。
然而,该方法是由 mixin M 实现的
。此外,如果我将 B 更改为:
@Mixin(M)
class B extends A {
def foo = 13
}
然后我得到打印输出:
11
12
而不是:
13
12
这是我所期望的,并且似乎证明 M 提供了可接受的实现 ?
那么,为什么 groovy 不喜欢使用 mixin M 来满足抽象类 A,我做错了什么
I have three Groovy classes: M, A, & B. B exists as a mixin of M and an extension of the abstract class A:
class M {
def foo = 11
def bar = 12
}
abstract class A {
abstract foo
}
@Mixin(M)
class B extends A {
}
def b = new B()
print "${b.foo}\n"
print "${b.bar}\n"
Attempting to run this causes Groovy to complain with:Can't have an abstract method in a non-abstract class. The class 'B' must be declared abstract or the method 'void setFoo(java.lang.Object)' must be implemented.
However, the method is implemented by the mixin M.
Furthermore, if I change B to be:
@Mixin(M)
class B extends A {
def foo = 13
}
Then I get the printout:
11
12
And not:
13
12
Which is what I expect and seems to prove that M provides an acceptable implementation of the abstract methods of A.
So, why isn't groovy happy with using the mixin M to satisfy the abstract class A, what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你没有做错什么。这是编译器在实际应用 AST 转换之前执行此检查的问题。您最好将此问题发布到他们的问题跟踪器上,网址为 http://jira.codehaus.org/ secure/BrowseProject.jspa?id=10242。
由于 @Mixin 转换实际上是由 Groovy++ AFAIK 的作者创建的,并且由于 Groovy++ 对类别、混合和特征有更多扩展支持,因此您可以期望这是一个有效的代码。你应该尝试一下。
You're doing nothing wrong. It's the problem of the compiler performing this check before actually applying the AST transformations. You better post this on their issue tracker at http://jira.codehaus.org/secure/BrowseProject.jspa?id=10242.
Since @Mixin transformation was actually created by the author of Groovy++ AFAIK, and since Groovy++ has much more extended support for categories, mixins and traits you could expect this to be a valid code there. You should try it.