Foo
以上级别)。
我觉得这有点尴尬,因为一旦category的方法被“混合”到目标类中,它们就不是静态的,但是在category类中它们是静态的。
// Define the category
class MyCategory {
void doIt() {
println "done"
}
void doIt2() {
println "done2"
}
}
// Mix the category into the target class
@Mixin (MyCategory)
class MyClass {
void callMixin() {
doIt()
}
}
// Test that it works
def obj = new MyClass()
obj.callMixin()
@Category
注释。例如,如果你只想申请
MyCategory
MyClass
(或其子类),定义为:
@Category(MyClass)
class MyCategory {
// Implementation omitted
}
@Mixin
(如上所述),您可以在运行时将它们混合在一起,而不是使用:
MyClass.mixin MyCategory
在你使用Grails时,
Bootstrap.groovy