Changes between Version 166 and Version 167 of j2p


Ignore:
Timestamp:
11/14/24 15:53:27 (11 days ago)
Author:
wouter
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • j2p

    v166 v167  
    131131
    132132===== Example workaround
    133 The following is an example of the tricky case: an inner class that calls methods in the parent class.
     133If the private class is a functional (eg Runnable, Predicate or Function), then a workaround method is to define an extraMethod in your class with the same signature (in/output) as the functional. Eg if you need a Function<String, Integer>, then define {{{private Integer extraMethod(String) {...} }}}, like this:
     134
     135
     136{{{
     137class Parent {
     138  ...
     139  Function<String, Integer> f = new Function<String, Integer>() {
     140        @Override
     141        public Integer apply(String t) {
     142          return 1;
     143        };
     144  ...
     145  use(f)
     146}
     147}}}
     148
     149
     150you can now replace this with
     151
     152{{{
     153class Parent {
     154  ...
     155  private Integer extraMethod(String t) {
     156    return 1
     157  }
     158
     159  ...
     160  use(this::extraMethod)
     161}
     162}}}
     163
     164
     165If the class you want to override is not a nice Functional, then it becomes more tricky. We recommend to create a separate class then
    134166Java has the arrow notation (x,y) -> methodcall(x,y) to write lambda expressions.
    135167This notation can also be translated. This feature is especially useful in the parent class to make references to methods the inner class needs.