Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy:如何获取调用静态方法的类型?_Groovy_Static Methods - Fatal编程技术网

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}"
}