Here you can see that I can't pass that string parameter
I somehow learned how to pass a method in the same class but, when I want to call it from another class, I can't pass the arguments.
CodePudding user response:
Several issues here:
- the
introduceMyself
method accepts a higher-order function, but in its usage it seems that you don't want the method itself, but its result. I'd suggest replacing thegetName
parameter withname
parameter and convert it to a regular string which will be provided externally by an invocation of said method. - In line 6, you have a syntax error. Since you're using an instance -
person
, you can replace the :: with a . like so:person.myNameIs("michael")
. It's not working because :: passes the method itself, which you don't really want. Again, your code implies that you need to pass the result of the method and not the method itself.
Finally, you can pass higher order functions, but you can't pass them "with an argument". You can do one of 3 things:
- Invoke the HOF locally and pass the result of the invocation
- Pass the HOF and the argument and invoke them wherever it is you need to.
- Pass the HOF and let the target pass whatever argument it chooses.