Groovy:如何获取调用静态方法的类型?
当使用子类调用在基类上定义的静态方法时,如何查找在子类类型上调用的静态方法Groovy:如何获取调用静态方法的类型?,groovy,static-methods,Groovy,Static Methods,当使用子类调用在基类上定义的静态方法时,如何查找在子类类型上调用的静态方法 class Base { static def method(){ println "class on which this method is called? ${this}" } } class Child extends Base {} Child.method() 在上面的代码中,这个正确地指向了基类。我认为这不能用实际的静态方法来完成,但是一个很好的替代方法是使用groov
class Base {
static def method(){
println "class on which this method is called? ${this}"
}
}
class Child extends Base {}
Child.method()
在上面的代码中,
这个
正确地指向了基类。我认为这不能用实际的静态方法来完成,但是一个很好的替代方法是使用groovy expandoMetaClass来添加静态闭包方法。在上述闭包中,您可以作为委托访问调用类。即
现在调用Base.anotherMethod()将使委托引用Base,调用Child.anotherMethod将使委托指向Child
Base.metaClass.static.anotherMethod = {
println "class on which another method is called? ${delegate}"
}