How to instantiate an abstract Java class with constructor parameter with groovy closure

I am trying to instantiate an abstract Java class from my Groovy code. Given the following abstract Java class (irrelevant processing is excluded from the class):

public abstract class StackOverflow{
  public abstract String answerMe();
}

I can easily create an instance in Groovy this way, and the call answerMe()will produce the correct output:

StackOverflow stack = [answerMe : { "Answer" }] as StackOverflow

Now, if I change the class StackOverflowby adding the String parameter to the constructor as follows:

public abstract class StackOverflowStr{
  public StackOverflowStr(String s){}
  public abstract String answerMe();
}

I really don't know how to instantiate an object, I've tried a lot, but it seems like I can't find the correct syntax, does anyone have any tips?

+5
source share
2 answers

You can create an instance in the classic Java style:

StackOverflowStr stack = new StackOverflowStr("javaish"){
    String answerMe() {"answer"}
}
+2

: .

- , .

, . -)

+3

All Articles