“返回此”的类型在 Groovy @Mixin 中
我有一个 mixin 类,它捆绑了不具有共同遗产的不同类型的功能。混合是使用@Mixin注释应用的,因此它是在编译时处理的。
一些 mixin 方法返回 this 作为方法调用的结果。问题是 this 是混合类型,而不是基类的类型。当我想在应用程序的其余部分中键入时,会抛出 ClassCastException ,表示混合类型无法转换为基本类型。
在下面的示例代码中,return this
返回 AMixin 类型的对象,而不是 BaseClass 类型的对象。
如何让 return this
返回 BaseClass 类型的对象而不是 AMixin 类型的对象?
class AMixin {
def getWhatIWant(){
if(isWhatIwant){
return this
} else {
getChildWhatIWant()
}
}
def getChildWhatIWant(){
for (def child in childred) {
def whatIWant = child.getWhatIWant()
if (whatIWant) {
return whatIWant
}
}
return null
}
}
@Mixin(AMixin)
class BaseClass {
boolean isWhatiWant
List<baseClass> children
}
I have a mixin class that bundles functionality for different types that do not share a common heritage. The mixing is applied using the @Mixin annotation, so it is handled at compile time.
Some of the mixin methods return this as the result of a method call. The problem is that the this is of the mixing type and not the type of the base class. When I want to work typed in the rest of the application a ClassCastException is thrown saying that the mixing type can not be cast to the base type.
In the example code below return this
returns an object of type AMixin instead of an Object of type BaseClass.
How can I have return this
return an object of type BaseClass instead of an object of type AMixin?
class AMixin {
def getWhatIWant(){
if(isWhatIwant){
return this
} else {
getChildWhatIWant()
}
}
def getChildWhatIWant(){
for (def child in childred) {
def whatIWant = child.getWhatIWant()
if (whatIWant) {
return whatIWant
}
}
return null
}
}
@Mixin(AMixin)
class BaseClass {
boolean isWhatiWant
List<baseClass> children
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚遇到了同样的情况。我通过将具体类中的“this”设置为具体类中的私有变量“me”并在 Mixin 类中返回“me”来解决这个问题。例如:
我觉得这有点笨拙,但我认为它比其他解决方案简单得多。我个人需要能够在多个类中使用相同的 Mixin,如果您无法将多个类别分配给单个 Mixin 类,那么听起来其他建议的解决方案将不允许这样做。
I just ran into this same situation. I solved it by setting 'this' from the concrete class into a private variable 'me' inside the concrete class and return 'me' in the Mixin classes. For example:
I feel like it's a bit kludgy, but I think it's a lot simpler than this other solution. I personally need the ability to use the same Mixin in multiple classes, and it sounds like this other proposed solution would not allow for that if you cannot assign multiple Categories to a single Mixin class.
我创建了 Base 类,并将类别添加到 AMixin 类中,并且 BaseClass 从 Base 扩展而来......(http://groovy.codehaus.org/Category+and+Mixin+transformations)
在 GroovyConsole 中执行此操作我得到
BaseClass@39c931fb
编辑只是虚拟类。我知道它的工作原理非常尴尬......我确信 Guillaume Laforge 可以回答这个问题作品...
I created the class Base added the category to the AMixin Class and the BaseClass extends from Base.....(http://groovy.codehaus.org/Category+and+Mixin+transformations)
Executed this in GroovyConsole I get
BaseClass@39c931fb
EDIT just a DummyClass. I know it's very awkward that It works....I'm sure Guillaume Laforge could answer how this works...