Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 - Fatal编程技术网

Groovy:如何从子类方法中的闭包引用超类方法?

Groovy:如何从子类方法中的闭包引用超类方法?,groovy,Groovy,在test.groovy中给出以下设置: class Main { public static void main(String ... args) { new Child().foo() } public static class Parent { def foo() { println 'from parent' } } public static class Child extends Parent { def foo()

test.groovy
中给出以下设置:

class Main {
  public static void main(String ... args) {
    new Child().foo()
  }

  public static class Parent {
    def foo() {
      println 'from parent'
    }
  }

  public static class Child extends Parent {
    def foo() {
      // def superRef = super.&foo  // needed to try what’s commented below
      def myClosure = {
        super.foo()  // doesn’t work, neither does anything of the following:
        // this.super.foo()
        // Child.super.foo()
        // Child.this.super.foo()
        // superRef()
        println 'from child'
      }
      myClosure()
    }
  }
}
当我运行
groovy test.groovy
(使用groovy 2.5.4和至少我尝试过的所有其他版本)时,我得到以下错误:

catch:groovy.lang.MissingMethodException:没有方法的签名:static Main.foo()适用于参数类型:()值:[]
可能的解决方案:any()、find()、use([Ljava.lang.Object;)、is(java.lang.Object)、any(groovy.lang.Closure)、find(groovy.lang.Closure)
groovy.lang.MissingMethodException:没有方法的签名:static Main.foo()适用于参数类型:()值:[]
可能的解决方案:any()、find()、use([Ljava.lang.Object;)、is(java.lang.Object)、any(groovy.lang.Closure)、find(groovy.lang.Closure)
主要$Child.methodMissing(test.groovy)
在Main$Child$\u foo\u closure1.doCall(test.groovy:16)
在Main$Child$\u foo\u closure1.doCall(test.groovy)中
在Main$Child.foo(test.groovy:23)
在主$Child$foo.call处(未知源)
at Main.Main(test.groovy:3)
如何从子类(
Child.foo
)的相应方法所包含的闭包(
myClosure
)中引用超类方法(
Parent.foo


(背景:我的实际代码中的闭包需要执行类似于
myCloseable.withCloseable{super.foo();…}
)的操作)

一旦闭包-它是另一个类(就java而言),您不能从另一个类访问super方法

IHMO唯一的方法:

class Main {
  public static void main(String ... args) {
    new Child().foo()
  }

  public static class Parent {
    def foo() {
      println 'from parent'
    }
  }

  public static class Child extends Parent {
    def superFoo(){
        super.foo()
    }
    def foo() {
      def myClosure = {
        superFoo()
        println 'from child'
      }
      myClosure()
    }
  }
}

感谢这一解决方案,+1。我仍然想知道是否还有更优雅的解决方案?我知道闭包是另一个类实例,但既然我可以访问闭包之外的其他字段/方法/…为什么我不能访问超级方法?你不能在java中这样做-所以你不能在groovy中这样做。只要尝试转换将代码转换为java,而不是使用Runnable implementation->compile error关闭。也许您可以重新设计方法/Close/Inheritation使其优雅……这是不正确的,在java中是可能的。请尝试示例代码,实现
Child.foo
如下:
Runnable myClosure=()->{Child.super.foo();System.err.println(“from Child”);};myClosure.run();